开发者

How much statement can be made conditional in MySQL?

the MySQL manual says that a CASE statement (the WHEN part) can contain a statement_list.

How much statement is that to be exact? I need to perform a pretty big query which I would like to parametrize. However this also implies that I need to change tables that are being joined. I've read that this cannot be done using CASE statements, so I'm looking for a solution to not have to re-create the stored procedure for every parameter value that there is...

UPDATE I tried to solve it using a CURSOR. I have defined a CURSOR for every object I have. I would like to use IF-ELSE-statements to choose which cursor to open, loop and close. However I cannot get the syntax right...

All curso开发者_开发知识库rs and variables (usb, obj, mm, stamp) are declared with different names, but I've shortened them for reading....

if OBJECTTYPE = 1 then
open CUR_1_BUILDING;
LOOP1: loop
    fetch CUR_1_BUILDING into usb, obj, mm, stamp;
    if no_more_rows then
        close CUR_1_BUILDING;
        leave loop1;
    end if;
    INSERT INTO ObjectCache (usb, obj, mm, date_checked) VALUES (usb, obj, mm, now());
end loop LOOP1;
    CLOSE CUR_1_BUILDING;
else if OBJECTTYPE = 2 then
open CUR_2_CITY;
LOOP2: loop
    fetch CUR_2_CITY into usb, obj, mm, stamp;
    if no_more_rows then
        close CUR_2_CITY;
        leave loop2;
    end if;
    INSERT INTO ObjectCache (usb, obj, mm, date_checked) VALUES (usb, obj, mm, now());
end loop LOOP2;
    close CUR_2_CITY;
end if;

Is this is any way possible to do using CASE statements?

BEGIN
    CASE
        when OBJECTTYPE = 1
            INSERT INTO ObjectCache SELECT id FROM Building
        when OBJECTTYPE = 2
            INSERT INTO ObjectCache SELECT id FROM City
    END CASE;
END


You could use IF ELSE or you could build and execute dynamic SQL.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜