简体   繁体   中英

How to Create Stored Procedure in DB2 with Data Studio 4.1.3 client

CREATE OR REPLACE PROCEDURE SPLoginAuditHistory_Login
(
    IN @Menuid int,
    IN @UserID numeric,
    IN @UserName varchar,   
    OUT @Result int
)
    DYNAMIC RESULT SETS 1
P1: BEGIN       
    DECLARE @err int;

    BEGIN TRANSACTION
    BEGIN
        INSERT INTO Login_Audit_History(Menuid,UserID,UserName,LoginTime)
        VALUES(@Menuid,@UserID,@UserName,Current DATE)
    END 
    SELECT @err=@@Error if @err<>0 goto Fail

    SELECT @Result=0
    COMMIT TRANSACTION
    RETURN

    Fail:
    SELECT @Result=1
    ROLLBACK TRANSACTION
    RETURN  

END P1;

Your question has nothing to do with IBM Data Studio, but instead it has everything to do with understanding Db2 procedural SQL syntax.

If your Db2 server runs on Linux/Unix or Microsoft-Windows then with correct configuration you can write your SQL procedures either in ANSI SQL PL syntax, or alternatively in Oracle style PL SQL syntax.

Your syntax does not comply with either of those styles of SQL that Db2-LUW understands.

If you are migrating the code from a different RDBMS, take the time to learn about the conversion tools for migration to Db2 that are available from different suppliers.

Some of your mistakes are below, so for each of your mistakes, study the correct Db2 documentation until you understand what's needed:

  • Variable names must not begin with @

  • Explicit transaction control, COMMIT [ WORK ], or ROLLBACK [ WORK ] [ TO SAVEPOINT ...]

  • If you want to have non-default error checking and error handling then you should use exception handlers, and/or explicit SQLCODE checking.

  • If you want to return a result-set, you must explicitly define and open a cursor before returning from the stored procedure

  • the end of the stored procedure needs a different delimiter than semi-colon. For example, many people use @ or ! to indicate the end of a block of compound-SQL. You need to configure your GUI to tell it which delimiter to use.

Learn from examples that are included with your Db2 server, and they are also online (be sure to use the correct version of Db2 documentation to match your Db2 server version and platform). For Db2 Linux/Windows v11.1 the samples are here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM