【发布时间】:2018-09-07 16:36:41
【问题描述】:
我有一个存储过程,我调用一个不同的存储过程来创建一个临时表,然后在 while exists 循环中使用该临时表。 当我在 while 循环中使用字段名称时,我收到一个错误,即临时表中的字段之一在它存在时不存在。
下面是代码:
call GetProc1 (0, 1, 5, 111);
while exists
(
select GlobalMarketDesc, MarketFamilyName, Country, rank from tmpRanks;
)
do
begin
If GlobalMarketDesc = 'United States' then
set strEventType = concat(GlobalMarketDesc, ' - ', MarketFamilyName);
Else
set strEventType = concat(GlobalMarketDesc, ' - ', Country, ' - ', MarketFamilyName);
end if;
end;
end while;
错误错误代码:1054 Unknown column 'GlobalMarketDesc' in 'field list'
当我更改为光标时,我仍然收到上述错误。这是我使用光标的新代码:
DECLARE no_more_records INT;
DECLARE cur_EventRanks CURSOR FOR
select * from tmpEventRanks;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_records=1;
set no_more_records = 0;
open cur_EventRanks;
cur_Loop: while (no_more_records=0)
do
begin
-- Set up the Event Type field
If GlobalMarketDesc = 'United States' then
set strEventType = concat(GlobalMarketDesc, ' - ', MarketFamilyName);
Else
set strEventType = concat(GlobalMarketDesc, ' - ', Country, ' - ', MarketFamilyName);
end if;
end while cur_Loop;
close cur_EventRanks;
【问题讨论】:
-
您希望
GlobalMarketDesc来自If GlobalMarketDesc = ...中的哪个位置?您所展示的任何内容都没有声明或更改变量......并且您的循环将永远或永远迭代;EXISTS不会从结果集中“拉一行”。要在 MySQL 中迭代结果,您需要一个 CURSOR。 -
子例程 GetProc1 用数据创建一个临时表。我试图不使用游标,因为我知道它使用大量内存并且我的子程序很复杂。如果您知道任何其他方式,请告诉我
-
我把它改成了一个游标,我仍然得到同样的错误。
-
如果您将代码与文档here 中的示例进行比较;您会注意到您缺少任何类型的
FETCH。根据我的经验,最好遵循文档示例建立的一般模式。 -
我看到了……我已经更正了代码。我使用光标发现了问题。我需要声明所有变量;我现在正在测试中。感谢您的帮助。
标签: mysql stored-procedures while-loop do-while