问题:写一个存储过程实现更新数据库中所有表的某一特定字段,并且只更新这个字段中的值是满足'60__A'??
我现在写了一个存储过程,执行是提示编译通过(Compiledsuccessfully),但是我查询数据库时,发现数据还是没有update啊?望哪位能帮忙看看我写的存储过程...
我现在写了一个存储过程,执行是提示编译通过(Compiled successfully),但是我查询数据库时,发现数据还是没有update啊?望哪位能帮忙看看我写的存储过程有什么问题吗?或者也可以帮我重新写一个。具体的存储过程如下:
create or replace procedure pro_test as cursor c is
select table_name from user_all_tables where table_name in (select table_name from user_tab_columns where column_name='TRAINNUM');
tablename varchar2(100);
strinsert varchar2(1000);
begin
open c;
loop
fetch c into tablename;
strinsert:= 'update '||tablename||' set trainnum=replace(trainnum,''C'',''A'') where trainnum like ''60__C'' ';
execute immediate strinsert;
end loop;
commit;
close c;
end;
我补充一下,我是说错了,问题应该是这样的:应该是字段的值满足'60__C',而不是原来的字段满足'60__A'?
此外,我要补充一下,还有的我用的是PL/SQL Developer的工具。
这个问题太困扰了,谁能帮忙解决一下啊,解决出来以后,必有重赏啊。 展开
create or replace procedure pro_test as cursor c is
select table_name from user_all_tables where table_name in (select table_name from user_tab_columns where column_name='TRAINNUM');
tablename varchar2(100);
strinsert varchar2(1000);
begin
open c;
loop
fetch c into tablename;
strinsert:= 'update '||tablename||' set trainnum=replace(trainnum,''C'',''A'') where trainnum like ''60__C'' ';
execute immediate strinsert;
end loop;
commit;
close c;
end;
我补充一下,我是说错了,问题应该是这样的:应该是字段的值满足'60__C',而不是原来的字段满足'60__A'?
此外,我要补充一下,还有的我用的是PL/SQL Developer的工具。
这个问题太困扰了,谁能帮忙解决一下啊,解决出来以后,必有重赏啊。 展开
展开全部
create procedure pro_test
as
declare @tablename varchar(100),
@strinsert varchar(1000)
DECLARE @_CURSOR_SQL CURSOR
SET @_CURSOR_SQL = CURSOR FOR
select table_name from user_all_tables where table_name in (select table_name from user_tab_columns where column_name='TRAINNUM')
open @_CURSOR_SQL
fetch next from @_CURSOR_SQL into @tablename
while @@FETCH_STATUS = 0
begin
set @strinsert = 'update ' + '||' + @tablename + '||' + ' set trainnum=replace(trainnum,''C'',''A'') where trainnum like '+ '60__C' + ''
execute @strinsert
--重新循环
FETCH NEXT FROM @_CURSOR_SQL into @tablename
end
CLOSE @_CURSOR_SQL
DEALLOCATE @_CURSOR_SQL
as
declare @tablename varchar(100),
@strinsert varchar(1000)
DECLARE @_CURSOR_SQL CURSOR
SET @_CURSOR_SQL = CURSOR FOR
select table_name from user_all_tables where table_name in (select table_name from user_tab_columns where column_name='TRAINNUM')
open @_CURSOR_SQL
fetch next from @_CURSOR_SQL into @tablename
while @@FETCH_STATUS = 0
begin
set @strinsert = 'update ' + '||' + @tablename + '||' + ' set trainnum=replace(trainnum,''C'',''A'') where trainnum like '+ '60__C' + ''
execute @strinsert
--重新循环
FETCH NEXT FROM @_CURSOR_SQL into @tablename
end
CLOSE @_CURSOR_SQL
DEALLOCATE @_CURSOR_SQL
更多追问追答
追问
十分感谢您的回答,但是您给我修改后的代码是有问题编译通不过啊。我给您截个图吧。还有,我原来的怎么能通过编译,但是运行完以后数据库中所有表中trainnum的字段中包含'60__C'还是没有改变成'60__A'啊?
追答
你是用什么工具调试的,我是在sql server里面写存储过程的;
还有'update '||tablename||' set trainnum=replace(trainnum,''C'',''A'') where trainnum like ''60__C'' ';
中可能使用游标的时候tablename传值不正确,你可以先用print函数把tablename打印出来看看
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询