Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Proc Question

Status
Not open for further replies.

chesl73

Programmer
Oct 3, 2008
1
AU
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
 
Use a PREPARE stm first.
Code:
...
DECLARE SQLSTM  VARCHAR (2048);
SET SQLSTM = 'SELECT * FROM TABLE1 WHERE ' || ADDWHERE; 
PREPARE S1 FROM SQLSTM;
DECLARE C1 SURSOR FOR S1;
OPEN C1;
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top