請幫忙看看為什麼會報錯,小弟首次編寫這種類型的預存程序:
delimiter $$
create procedure mydb()
begin
declare dbname varchar(50);
declare stop int default 0;
declare cur cursor for (select name from name); //name表中存放的是資料庫的名稱
declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop = null;
open cur;
fetch cur into dbname; //遍歷資料庫的名稱
while ( stop is not null) do
begin
declare name varchar(200);
declare stop1 int default 0;
declare cur1 cursor for (select prounit_name from dbname.prounit); //获取prounit表中prounit_name字段内容
declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop1 = null;
open cur1;
fetch cur1 into name;
while ( stop1 is not null) do
update prounit set name='abc';
fetch cur1 into name;
end while;
close cur1;
end ;
fetch cur into dbname;
end while;
close cur;
end ;
delimiter $$
在執行預存程序error:ERROR 1146 (42S02): Table 'dbname.prounit' doesn't exist
why?????
提示很明顯,表不存在
declare dbname varchar(50);
名字覆蓋了?說幾個文法問題吧.
while ( stop is not null) do
中的stop
應該遊標dbname
才對,對遊標進行判斷.不然對stop沒有引用過最後一個
end;
,前面你定義了delimiter $$`以`$$
為結束符,應該改為end$$
,最後不要忘了結尾改成delimiter ;