对于模糊的标题抱歉,这是我的问题。我有DB2的存储过程,我试图转换为MySQL。我想知道我是否可以将游标声明中的SELECT语句写为字符串变量。例如,对于DB2,我有这个:
(...)
-- Declare cursors
DECLARE c_very_init CURSOR WITH RETURN FOR s_very_init;
DECLARE c_date CURSOR WITH RETURN FOR s_date;
DECLARE CONTINUE HANDLER FOR not_found
SET at_end = 1;
-- In case the_date is 0, retrieve the first date
IF the_date = 0 THEN
SET sql_end_date = '
SELECT DATE
FROM ACCOUNTS
WHERE REF = ''' || the_ref || '''
ORDER BY ID ASC FETCH FIRST 1 ROWS ONLY';
PREPARE s_date FROM sql_end_date;
OPEN c_date;
FETCH FROM c_date INTO data_ins;
SET the_last_date = data_ins;
CLOSE c_date;
ELSE
SET the_last_date = the_date;
END IF;
-- Get the 'very' initial value
SET sql_very_init = '
SELECT in, out
FROM MOVEMENTS
WHERE REF = ''' || the_ref || '''
AND DATE < ' || the_last_date;
PREPARE s_very_init FROM sql_very_init;
OPEN c_very_init;
FETCH FROM c_very_init INTO dare, avere;
-- Loop through the results
(...)
我声明了一个c_very_init
游标,但是在SP中的游标声明时,我仍然不知道完整的select语句,因为我需要获取(如果需要)the_last_date
值。我似乎不能这样做:
DECLARE c_very_init CURSOR WITH RETURN FOR s_very_init;
使用MySQL,语法直接在声明中使用语句:
DECLARE c_very_init CURSOR FOR SELECT blaablaa...;
我错了吗?
谢谢。 法比安
答案 0 :(得分:0)
不,你不能用这种方式声明游标。但如果'the_ref'是一个变量,你就可以这样做 -
...
DECLARE the_ref INT DEFAULT 10;
DECLARE cur1 CURSOR FOR SELECT column1 FROM table1 WHERE column1 = the_ref;
...