Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / January 2007

Tip: Looking for answers? Try searching our database.

Stop a stored procedure - newbie

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Justin - 29 Jan 2007 14:09 GMT
Within a stored procedure, I am looking for the command to end
execution.

I assume end, return, exit, quit.  All seem to fail.

In the event a condition occurs, I want to stop all processing within
the stored procedure.
Serge Rielau - 29 Jan 2007 14:18 GMT
> Within a stored procedure, I am looking for the command to end
> execution.
[quoted text clipped - 3 lines]
> In the event a condition occurs, I want to stop all processing within
> the stored procedure.

Is that an SQL Procedure? RETURN 0 should do the trick. Define "fail".

Signature

Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

Justin - 29 Jan 2007 14:41 GMT
Failure occurs when creating the stored procedure...  (prior to
running the stored proc)

With return 0, I receive the failure:

An unexpected token "return" was found following "".  Expected tokens
may include:  "".  SQLState=42601

> > Within a stored procedure, I am looking for the command to end
> > execution.
[quoted text clipped - 8 lines]
> DB2 Solutions Development
> IBM Toronto Lab
Knut Stolze - 29 Jan 2007 16:43 GMT
> Failure occurs when creating the stored procedure...  (prior to
> running the stored proc)
[quoted text clipped - 3 lines]
> An unexpected token "return" was found following "".  Expected tokens
> may include:  "".  SQLState=42601

Please show us the complete SQL statement that creates the procedure.  It
seems that you have some sort of syntax error somewhere.

Signature

Knut Stolze
DB2 z/OS Utilities Development
IBM Germany

Justin - 29 Jan 2007 21:50 GMT
The issue is with the placement of return 0.

Here is a procedure against sample database to reproduce the issue:
(Please excuse the spacing... )

CREATE PROCEDURE USERID.STAFF_UPDATE ( )
 SPECIFIC USERID.STAFF_UPDATE
 LANGUAGE SQL
 NOT DETERMINISTIC
 CALLED ON NULL INPUT
 MODIFIES SQL DATA
 INHERIT SPECIAL REGISTERS
 us: BEGIN

    DECLARE sqlstate CHAR(5) DEFAULT '00000';
    DECLARE sqlcode int default 0;
    DECLARE a char;
    DECLARE b char;
    DECLARE sqltype char;

    DECLARE c_query CURSOR WITH HOLD FOR
    select p.PROJNO, p.PROJNAME
           from PROJECT p, staff s
           where s.id = cast(p.respemp as int)
           order by s.dept;

    DECLARE CONTINUE HANDLER FOR NOT FOUND

    BEGIN

        IF (sqltype = 'U') THEN
            return 0;
                                END IF;
    COMMIT;
    END;

    OPEN c_query;
    FETCH FROM c_query INTO a, b;

    While (sqlstate = '00000')
    do
                     sqltype = 'U';
         IF (a = 'MA2113')  THEN
             Insert into applog (Description) values ('Project identified
');
         else
             Insert into applog (Description) values ('valid projno = '||
char(a));
         end if;

                     FETCH FROM c_query INTO a, b;

    END WHILE;
END;

> > Failure occurs when creating the stored procedure...  (prior to
> > running the stored proc)
[quoted text clipped - 9 lines]
> DB2 z/OS Utilities Development
> IBM Germany
Knut Stolze - 30 Jan 2007 20:01 GMT
> The issue is with the placement of return 0.
>
[quoted text clipped - 29 lines]
>       COMMIT;
>    END;

The problem is that RETURN is not a statement that counts as SQL-procedure
statement.  Therefore, it cannot be embedded in a compound statement in an
exception handler.  Besides, you state that your handler is a CONTINUE
handler, i.e. it does something and then _continues_ in the procedure.  You
want to break that...

> OPEN c_query;
> FETCH FROM c_query INTO a, b;
[quoted text clipped - 14 lines]
> END WHILE;
> END;

If you look at the above logic, how about rephrasing it to:

FOR query_loop AS c_query
  CURSOR WITH HOLD FOR
     SELECT p.projno, p.projname
     FROM   project AS p, staff AS s
     WHERE  s.id = cast(p.respemp as int)
     ORDER BY s.dept DO
  INSERT INTO applog(description)
     VALUES (CASE
                WHEN p.projno = 'MA2113'
                THEN 'Project identified'
                ELSE 'valid projno = ' || CHAR(p.projno)
             END);
END FOR

The loop will terminate when no record is found.  Additionally, you don't
need the "sqltype", "a", and "b" variables and no handler for NOT FOUND
conditions.  Even better yet, using a CASE expression instead of an IF
statement has the distinct advantage that DB2 can better optimize the
overall statement.

p.s: The variables "a" and "b" not speaking names.  You should use names
that make it easy to understand what's going on.

Signature

Knut Stolze
DB2 z/OS Utilities Development
IBM Germany

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.