开发者

MySQL Stored Procedures : cursor declaration

Sorry for the vague title, here is my problem. I have stored procedures for DB2 that i try to convert for MySQL. I'd like to know if i can write the SELECT statement in the cursor declaration as a string variable. For example with DB2 i have this :

(...)
-- 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;
    OP开发者_Go百科EN 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
(...)

I declare a c_very_init cursor, but at the time of the cursor declaration in the SP i still don't know the full select statement because i need to fetch (if necessary) the the_last_date value. It seems i can't do this :

DECLARE c_very_init CURSOR WITH RETURN FOR s_very_init;

with MySQL, the syntax being with the statement directly in the declaration :

DECLARE c_very_init CURSOR FOR SELECT blaablaa...;

Am i wrong?

Thank you. fabien.


No, you cannot declare cursors in this way. But if 'the_ref' is a variable, you could do it like this -

...
DECLARE the_ref INT DEFAULT 10;
DECLARE cur1 CURSOR FOR SELECT column1 FROM table1 WHERE column1 = the_ref;
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜