Hi all,
I would like to write a stored proc that takes a IN parameter(s) and then uses this to dynamically change the WHERE clause of my sql.
example is something like:
CREATE PROCEDURE BLAH(IN MYVAR VARCHAR(50),IN MYVAL VARCHAR(50))
....
....
DECLARE ADDWHERE VARCHAR(100);
IF MYVAR='VIEWCAT' THEN
SET ADDWHERE = 'TABLE1.FIELD1='''|| MYVAL || '''';
ELSE IF MYVAR='VIEWDOG' THEN
SET ADDWHERE = 'TABLE1.FIELD2='''|| MYVAL || '''';
END IF;
DECLARE C1 CURSOR FOR
'SELECT * FROM TABLE1 WHERE ' || ADDWHERE;
----------
Is this possible? I've tried and can't get it to work. Not sure that the single quotes around the SELECT are correct and if I'd need to use some kind of PREPARE statement - just don't know. Any ideas?
Thanks
I would like to write a stored proc that takes a IN parameter(s) and then uses this to dynamically change the WHERE clause of my sql.
example is something like:
CREATE PROCEDURE BLAH(IN MYVAR VARCHAR(50),IN MYVAL VARCHAR(50))
....
....
DECLARE ADDWHERE VARCHAR(100);
IF MYVAR='VIEWCAT' THEN
SET ADDWHERE = 'TABLE1.FIELD1='''|| MYVAL || '''';
ELSE IF MYVAR='VIEWDOG' THEN
SET ADDWHERE = 'TABLE1.FIELD2='''|| MYVAL || '''';
END IF;
DECLARE C1 CURSOR FOR
'SELECT * FROM TABLE1 WHERE ' || ADDWHERE;
----------
Is this possible? I've tried and can't get it to work. Not sure that the single quotes around the SELECT are correct and if I'd need to use some kind of PREPARE statement - just don't know. Any ideas?
Thanks