Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2003
    Posts
    144

    Question Unanswered: Is it possible to execute a string within an SQL stored procedure?

    Hello!

    In SQL Server, you can do something like this within an stored procedure:

    declare @MyQuery
    Set @MyQuery='select * from mytable'
    execute @MyQuery

    Is is possible to do the same thing in DB2 v8.1? Perhaps by using a system stored procedure?

    Thanks a lot in advance.

  2. #2
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    Yes.

    You can do something like:

    DECLARE cursorx CURSOR WITH RETURN FOR s1;

    CASE Mode
    WHEN 0 THEN
    SET whereClause = '';
    WHEN 1 THEN
    SET whereClause = ' where (col1 is NULL) ';
    WHEN 2 THEN
    SET whereClause = ' where (col1 is NOT NULL) ';
    END CASE;


    SET stmt = 'select * from mytable ' || whereClause ;

    PREPARE s1 from stmt;

    OPEN cursorx;


    HTH
    Andy

  3. #3
    Join Date
    Mar 2003
    Posts
    144

    Talking

    Thank you very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •