views:

46

answers:

1

Hi, 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 cursors 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
+1  A: 

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

Cade Roux
But i have more than 2 OBJECTTYPES, does this mean I cannot use IF ELSE?
Ropstah
You can chain multiple IF ELSE IF ELSE IF ELSE IF etc...
Cade Roux
I tried this, but it doesn't seem to work on my complex query... I have a syntax error at line 96; (which is empty), the script goes till 95; I'm gonna try a little further, otherwise i'll post another sample
Ropstah
Please see the updated question...
Ropstah