-2

I am trying to execute the following SQL script inside a IBMDB2- however unable to get the syntactically correct script for IBMDB2;

BEGIN TRY

    BEGIN TRANSACTION

    if not exists (select * from AAA.BBBB WHERE COL1='[Param.1]' and COL2=[Param.2] and COL3='[Param.3]')
    begin
        INSERT INTO PRODFILE.MESUPPF (COL1, COL2, COL3, COL4,  COL5, COL6)
        VALUES( '[Param.1]', [Param.2], '[Param.3]', '[Param.4]' , '[Param.5]', '[Param.6]' )
    end
    else
    begin
        update AA.BB set COL1='[Param.4]',COL2='[Param.5]',COL3='[Param.6]'  WHERE COL4='[Param.1]' and COL5=[Param.2] and COL6='[Param.3]')
    end
    Select  'Success'  as Message, 'S' as MessageType

END TRY
BEGIN CATCH

    ROLLBACK TRANSACTION

    INSERT INTO ERROR_LOG (LOGTM,LOGTX) VALUES (getdate(),CAST(ERROR_LINE()AS VARCHAR(4)) + ERROR_MESSAGE())
    Select  'Error'  as Message, 'E' as MessageType

END CATCH
jmarkmurphy
  • 11,030
  • 31
  • 59
Rajeev
  • 27
  • 5
  • Db2 uses SQL. Do you mean an SQL script written for another database system? – data_henrik Mar 03 '20 at 12:05
  • @data_henrik no! Actually I am trying to execute the above sql script in an DB2 (AS400) system- however the above sql script doesn't execute. It gives errors pertaining to usage of TRY,IF etc. However the above same script WORKS FINE IN SQL SERVER – Rajeev Mar 03 '20 at 12:19
  • Does this answer your question? [DB2 Stored Procedure try catch](https://stackoverflow.com/questions/45618543/db2-stored-procedure-try-catch) – SMor Mar 03 '20 at 12:25
  • Did you try searching the internet for "db2 error handling"? – SMor Mar 03 '20 at 12:25
  • What flavor of DB2 are you talking about? DB2 for zOS (mainframe), DB2 for i (midrange), DB2 for LUW (Linux, Unix, Windows)? They are slightly different, and the correct documentation link will depend on the flavor you are using. – jmarkmurphy Mar 03 '20 at 13:55
  • @jmarkmurphy client is using IBMDB2 on windows – Rajeev Mar 03 '20 at 14:15
  • @Rajeev Ok, but the client is not what determines the dialect. The server determines that. I am now guessing that the DB2 server is located on Windows and you just misspoke when you said client. – jmarkmurphy Mar 03 '20 at 21:05
  • Here is the [DB2 for LUW documentation](https://www.ibm.com/support/knowledgecenter/SSEPGG_11.5.0/com.ibm.db2.luw.welcome.doc/doc/welcome.html) – jmarkmurphy Mar 03 '20 at 21:07

1 Answers1

0

DB2 for IBM i doesn't allow execution of compound sql statements outside of SQL routines.
Refer to CREATE PROCEDURE (SQL) on how to create such a routine with DB2 dialect of statements inside.

Mark Barinstein
  • 11,456
  • 2
  • 8
  • 16
  • Does this hold for DB2 LUW – jmarkmurphy Mar 03 '20 at 21:03
  • @jmarkmurphy If the question is on if Db2 for LUW has the same limitation, then the answer is: no. Db2for LUW does allow compound statements. But the SQL syntax is different from SQL Server, of course. [Compound SQL (compiled) statement](https://www.ibm.com/support/knowledgecenter/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0004239.html) – Mark Barinstein Mar 04 '20 at 07:14