Quote:
|
Originally Posted by ARWinner
My answer is probably yes. A lot depends on what you are trying to accomplish. I have numerous SPs that use all static SQL and work like that. Posting the SP and a description of what you are trying to accomplish would net you a better answer.
Andy
|
Ok, here is an example: this is a simple stored proc that is supposed to give information about tables/views (somehow like the sp_help sp in mssql).
Code:
create procedure sp_help
(
IN TableName varchar (255),
IN LibraryName varchar(255)
)
RESULT SETS 1
LANGUAGE SQL
BEGIN
DECLARE sqlstmt VARCHAR(1000);
DECLARE cur CURSOR WITH RETURN FOR s1;
IF LibraryName = '' THEN
SET LibraryName = 'CRMDATA';
END IF;
SET sqlstmt = '
SELECT *
FROM qsys2.syscolumns
WHERE table_schema LIKE ?
AND table_name LIKE ?';
SET TableName = UPPER(TableName);
PREPARE s1 FROM sqlstmt;
OPEN cur USING LibraryName, TableName;
END;
I need to declare the cursor
cur in the declarations section (so before I get the chance to define the SELECT query). In between I need to have some statements (alter some of the parameters).
If I would do something like:
Code:
IF LibraryName = '' THEN
SET LibraryName = 'CRMDATA';
END IF;
SET TableName = UPPER(TableName);
DECLARE cur CURSOR WITH RETURN FOR
SELECT *
FROM qsys2.syscolumns
WHERE table_schema LIKE LibraryName
AND table_name LIKE TableName;
OPEN cur;
It fails to compile and it doesn't like the declare cursor statement. From what I've read (in here:
www.redbooks.ibm.com/abstracts/sg246503.html) I understand that all declarations need to sit in a block at the beginning. The only way I found around this issue is to use dynamic sql as above.
Please let me know if there is any other better way to do it.