Get SQLCODE and SQLSTATE from stored procedure with executing a statement from variable












0















I have this:



CREATE OR replace PROCEDURE log_test
( IN QUERY VARCHAR(24576),
IN LOGTBL varchar(20) ) LANGUAGE SQL
BEGIN
DECLARE v_select_query VARCHAR(24576);
DECLARE v_query VARCHAR(24576);
DECLARE v_logtbl varchar(20);
DECLARE v_errormsg varchar(2048);
DECLARE v_time TIMESTAMP;
DECLARE v_temp_select varchar(1024);
DECLARE stmt STATEMENT;
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE CONTINUE HANDLER
FOR SQLEXCEPTION,SQLWARNING,NOT FOUND
SET v_sqlcode = SQLCODE;




set v_select_query = 'Set (?) = ('||QUERY||')';
set v_query = 'Set (?) = ('||QUERY||')';
set v_bezug = bezug;
set v_logtbl = logtbl;
set v_time = CURRENT TIMESTAMP;



PREPARE stmt from v_select_query;

EXECUTE stmt into v_temp_select;

END @


The statement query is



 select count(*) from testtbl;


and without the sqlstate catching it works.



The result is that I want the sqlcode saved in the variables when the statement is successful or fails. However now I only get an error message that after "" the unexpected token "".



Any ideas on how to fix this? I later want to log the sqlcode with an insert into another table.



DB2 Windows v10.5



Thanks for your help










share|improve this question

























  • If an SQL statement succeeds, SQLCODE is always zero. If an SQL statement fails, SQLCODE is not zero and SQLSTATE is set to something different than '00000'. As you have declared an EXIT HANDLER that simply sets a local variable then on SQL statement failure your sproc will exit immediately , meaning your exit handler as currently coded is useless although you can extend it to insert the sqlcode/sqlstate into another table (better to call an autonomous sproc for that purpose to ensure a separate UOW).

    – mao
    Nov 15 '18 at 12:28











  • Also you cannot have both a continue and an exit-handler for SQLEXCEPTION. You can have one of these.

    – mao
    Nov 15 '18 at 12:38











  • I deleted the exit handler. How can I get the procedure to work? It does not compile

    – Viking
    Nov 15 '18 at 12:42











  • Fix the syntax errors, there are several obvious ones. Put SQLCODE and SQLSTATE declarations first (before the statement declaration). Define the variables that are undefined (v_sqlcode, v_bezug etc).

    – mao
    Nov 15 '18 at 12:46


















0















I have this:



CREATE OR replace PROCEDURE log_test
( IN QUERY VARCHAR(24576),
IN LOGTBL varchar(20) ) LANGUAGE SQL
BEGIN
DECLARE v_select_query VARCHAR(24576);
DECLARE v_query VARCHAR(24576);
DECLARE v_logtbl varchar(20);
DECLARE v_errormsg varchar(2048);
DECLARE v_time TIMESTAMP;
DECLARE v_temp_select varchar(1024);
DECLARE stmt STATEMENT;
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE CONTINUE HANDLER
FOR SQLEXCEPTION,SQLWARNING,NOT FOUND
SET v_sqlcode = SQLCODE;




set v_select_query = 'Set (?) = ('||QUERY||')';
set v_query = 'Set (?) = ('||QUERY||')';
set v_bezug = bezug;
set v_logtbl = logtbl;
set v_time = CURRENT TIMESTAMP;



PREPARE stmt from v_select_query;

EXECUTE stmt into v_temp_select;

END @


The statement query is



 select count(*) from testtbl;


and without the sqlstate catching it works.



The result is that I want the sqlcode saved in the variables when the statement is successful or fails. However now I only get an error message that after "" the unexpected token "".



Any ideas on how to fix this? I later want to log the sqlcode with an insert into another table.



DB2 Windows v10.5



Thanks for your help










share|improve this question

























  • If an SQL statement succeeds, SQLCODE is always zero. If an SQL statement fails, SQLCODE is not zero and SQLSTATE is set to something different than '00000'. As you have declared an EXIT HANDLER that simply sets a local variable then on SQL statement failure your sproc will exit immediately , meaning your exit handler as currently coded is useless although you can extend it to insert the sqlcode/sqlstate into another table (better to call an autonomous sproc for that purpose to ensure a separate UOW).

    – mao
    Nov 15 '18 at 12:28











  • Also you cannot have both a continue and an exit-handler for SQLEXCEPTION. You can have one of these.

    – mao
    Nov 15 '18 at 12:38











  • I deleted the exit handler. How can I get the procedure to work? It does not compile

    – Viking
    Nov 15 '18 at 12:42











  • Fix the syntax errors, there are several obvious ones. Put SQLCODE and SQLSTATE declarations first (before the statement declaration). Define the variables that are undefined (v_sqlcode, v_bezug etc).

    – mao
    Nov 15 '18 at 12:46
















0












0








0








I have this:



CREATE OR replace PROCEDURE log_test
( IN QUERY VARCHAR(24576),
IN LOGTBL varchar(20) ) LANGUAGE SQL
BEGIN
DECLARE v_select_query VARCHAR(24576);
DECLARE v_query VARCHAR(24576);
DECLARE v_logtbl varchar(20);
DECLARE v_errormsg varchar(2048);
DECLARE v_time TIMESTAMP;
DECLARE v_temp_select varchar(1024);
DECLARE stmt STATEMENT;
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE CONTINUE HANDLER
FOR SQLEXCEPTION,SQLWARNING,NOT FOUND
SET v_sqlcode = SQLCODE;




set v_select_query = 'Set (?) = ('||QUERY||')';
set v_query = 'Set (?) = ('||QUERY||')';
set v_bezug = bezug;
set v_logtbl = logtbl;
set v_time = CURRENT TIMESTAMP;



PREPARE stmt from v_select_query;

EXECUTE stmt into v_temp_select;

END @


The statement query is



 select count(*) from testtbl;


and without the sqlstate catching it works.



The result is that I want the sqlcode saved in the variables when the statement is successful or fails. However now I only get an error message that after "" the unexpected token "".



Any ideas on how to fix this? I later want to log the sqlcode with an insert into another table.



DB2 Windows v10.5



Thanks for your help










share|improve this question
















I have this:



CREATE OR replace PROCEDURE log_test
( IN QUERY VARCHAR(24576),
IN LOGTBL varchar(20) ) LANGUAGE SQL
BEGIN
DECLARE v_select_query VARCHAR(24576);
DECLARE v_query VARCHAR(24576);
DECLARE v_logtbl varchar(20);
DECLARE v_errormsg varchar(2048);
DECLARE v_time TIMESTAMP;
DECLARE v_temp_select varchar(1024);
DECLARE stmt STATEMENT;
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE CONTINUE HANDLER
FOR SQLEXCEPTION,SQLWARNING,NOT FOUND
SET v_sqlcode = SQLCODE;




set v_select_query = 'Set (?) = ('||QUERY||')';
set v_query = 'Set (?) = ('||QUERY||')';
set v_bezug = bezug;
set v_logtbl = logtbl;
set v_time = CURRENT TIMESTAMP;



PREPARE stmt from v_select_query;

EXECUTE stmt into v_temp_select;

END @


The statement query is



 select count(*) from testtbl;


and without the sqlstate catching it works.



The result is that I want the sqlcode saved in the variables when the statement is successful or fails. However now I only get an error message that after "" the unexpected token "".



Any ideas on how to fix this? I later want to log the sqlcode with an insert into another table.



DB2 Windows v10.5



Thanks for your help







sql stored-procedures error-handling db2 execute






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 12:42







Viking

















asked Nov 15 '18 at 12:16









VikingViking

466




466













  • If an SQL statement succeeds, SQLCODE is always zero. If an SQL statement fails, SQLCODE is not zero and SQLSTATE is set to something different than '00000'. As you have declared an EXIT HANDLER that simply sets a local variable then on SQL statement failure your sproc will exit immediately , meaning your exit handler as currently coded is useless although you can extend it to insert the sqlcode/sqlstate into another table (better to call an autonomous sproc for that purpose to ensure a separate UOW).

    – mao
    Nov 15 '18 at 12:28











  • Also you cannot have both a continue and an exit-handler for SQLEXCEPTION. You can have one of these.

    – mao
    Nov 15 '18 at 12:38











  • I deleted the exit handler. How can I get the procedure to work? It does not compile

    – Viking
    Nov 15 '18 at 12:42











  • Fix the syntax errors, there are several obvious ones. Put SQLCODE and SQLSTATE declarations first (before the statement declaration). Define the variables that are undefined (v_sqlcode, v_bezug etc).

    – mao
    Nov 15 '18 at 12:46





















  • If an SQL statement succeeds, SQLCODE is always zero. If an SQL statement fails, SQLCODE is not zero and SQLSTATE is set to something different than '00000'. As you have declared an EXIT HANDLER that simply sets a local variable then on SQL statement failure your sproc will exit immediately , meaning your exit handler as currently coded is useless although you can extend it to insert the sqlcode/sqlstate into another table (better to call an autonomous sproc for that purpose to ensure a separate UOW).

    – mao
    Nov 15 '18 at 12:28











  • Also you cannot have both a continue and an exit-handler for SQLEXCEPTION. You can have one of these.

    – mao
    Nov 15 '18 at 12:38











  • I deleted the exit handler. How can I get the procedure to work? It does not compile

    – Viking
    Nov 15 '18 at 12:42











  • Fix the syntax errors, there are several obvious ones. Put SQLCODE and SQLSTATE declarations first (before the statement declaration). Define the variables that are undefined (v_sqlcode, v_bezug etc).

    – mao
    Nov 15 '18 at 12:46



















If an SQL statement succeeds, SQLCODE is always zero. If an SQL statement fails, SQLCODE is not zero and SQLSTATE is set to something different than '00000'. As you have declared an EXIT HANDLER that simply sets a local variable then on SQL statement failure your sproc will exit immediately , meaning your exit handler as currently coded is useless although you can extend it to insert the sqlcode/sqlstate into another table (better to call an autonomous sproc for that purpose to ensure a separate UOW).

– mao
Nov 15 '18 at 12:28





If an SQL statement succeeds, SQLCODE is always zero. If an SQL statement fails, SQLCODE is not zero and SQLSTATE is set to something different than '00000'. As you have declared an EXIT HANDLER that simply sets a local variable then on SQL statement failure your sproc will exit immediately , meaning your exit handler as currently coded is useless although you can extend it to insert the sqlcode/sqlstate into another table (better to call an autonomous sproc for that purpose to ensure a separate UOW).

– mao
Nov 15 '18 at 12:28













Also you cannot have both a continue and an exit-handler for SQLEXCEPTION. You can have one of these.

– mao
Nov 15 '18 at 12:38





Also you cannot have both a continue and an exit-handler for SQLEXCEPTION. You can have one of these.

– mao
Nov 15 '18 at 12:38













I deleted the exit handler. How can I get the procedure to work? It does not compile

– Viking
Nov 15 '18 at 12:42





I deleted the exit handler. How can I get the procedure to work? It does not compile

– Viking
Nov 15 '18 at 12:42













Fix the syntax errors, there are several obvious ones. Put SQLCODE and SQLSTATE declarations first (before the statement declaration). Define the variables that are undefined (v_sqlcode, v_bezug etc).

– mao
Nov 15 '18 at 12:46







Fix the syntax errors, there are several obvious ones. Put SQLCODE and SQLSTATE declarations first (before the statement declaration). Define the variables that are undefined (v_sqlcode, v_bezug etc).

– mao
Nov 15 '18 at 12:46














1 Answer
1






active

oldest

votes


















1














As per comments, fix the syntax errors in your code. The example below will compile for Db2-LUW, but there are other errors and problems in your code that you will find later with testing.



CREATE OR replace PROCEDURE log_test
( IN QUERY VARCHAR(24576),
IN LOGTBL varchar(20) )
LANGUAGE SQL
specific log_test
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE v_select_query VARCHAR(24576);
DECLARE v_query VARCHAR(24576);
DECLARE v_logtbl varchar(20);
DECLARE v_errormsg varchar(2048);
DECLARE v_time TIMESTAMP;
DECLARE v_temp_select varchar(1024);
DECLARE v_sqlcode INTEGER;
DECLARE v_sqlstate CHAR(5);
DECLARE v_bezug varchar(1024);

DECLARE stmt STATEMENT;
DECLARE CONTINUE HANDLER FOR SQLWARNING,NOT FOUND
SET v_sqlcode = SQLCODE;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
SET v_sqlstate = SQLSTATE;


set v_select_query = 'Set (?) = ('||QUERY||')';
set v_query = 'Set (?) = ('||QUERY||')';
set v_bezug = 'bezug';
set v_logtbl = logtbl;
set v_time = CURRENT TIMESTAMP;



PREPARE stmt from v_select_query;

EXECUTE stmt into v_temp_select;

END@





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319330%2fget-sqlcode-and-sqlstate-from-stored-procedure-with-executing-a-statement-from-v%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    As per comments, fix the syntax errors in your code. The example below will compile for Db2-LUW, but there are other errors and problems in your code that you will find later with testing.



    CREATE OR replace PROCEDURE log_test
    ( IN QUERY VARCHAR(24576),
    IN LOGTBL varchar(20) )
    LANGUAGE SQL
    specific log_test
    BEGIN
    DECLARE SQLCODE INTEGER DEFAULT 0;
    DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
    DECLARE v_select_query VARCHAR(24576);
    DECLARE v_query VARCHAR(24576);
    DECLARE v_logtbl varchar(20);
    DECLARE v_errormsg varchar(2048);
    DECLARE v_time TIMESTAMP;
    DECLARE v_temp_select varchar(1024);
    DECLARE v_sqlcode INTEGER;
    DECLARE v_sqlstate CHAR(5);
    DECLARE v_bezug varchar(1024);

    DECLARE stmt STATEMENT;
    DECLARE CONTINUE HANDLER FOR SQLWARNING,NOT FOUND
    SET v_sqlcode = SQLCODE;
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    SET v_sqlstate = SQLSTATE;


    set v_select_query = 'Set (?) = ('||QUERY||')';
    set v_query = 'Set (?) = ('||QUERY||')';
    set v_bezug = 'bezug';
    set v_logtbl = logtbl;
    set v_time = CURRENT TIMESTAMP;



    PREPARE stmt from v_select_query;

    EXECUTE stmt into v_temp_select;

    END@





    share|improve this answer




























      1














      As per comments, fix the syntax errors in your code. The example below will compile for Db2-LUW, but there are other errors and problems in your code that you will find later with testing.



      CREATE OR replace PROCEDURE log_test
      ( IN QUERY VARCHAR(24576),
      IN LOGTBL varchar(20) )
      LANGUAGE SQL
      specific log_test
      BEGIN
      DECLARE SQLCODE INTEGER DEFAULT 0;
      DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
      DECLARE v_select_query VARCHAR(24576);
      DECLARE v_query VARCHAR(24576);
      DECLARE v_logtbl varchar(20);
      DECLARE v_errormsg varchar(2048);
      DECLARE v_time TIMESTAMP;
      DECLARE v_temp_select varchar(1024);
      DECLARE v_sqlcode INTEGER;
      DECLARE v_sqlstate CHAR(5);
      DECLARE v_bezug varchar(1024);

      DECLARE stmt STATEMENT;
      DECLARE CONTINUE HANDLER FOR SQLWARNING,NOT FOUND
      SET v_sqlcode = SQLCODE;
      DECLARE EXIT HANDLER FOR SQLEXCEPTION
      SET v_sqlstate = SQLSTATE;


      set v_select_query = 'Set (?) = ('||QUERY||')';
      set v_query = 'Set (?) = ('||QUERY||')';
      set v_bezug = 'bezug';
      set v_logtbl = logtbl;
      set v_time = CURRENT TIMESTAMP;



      PREPARE stmt from v_select_query;

      EXECUTE stmt into v_temp_select;

      END@





      share|improve this answer


























        1












        1








        1







        As per comments, fix the syntax errors in your code. The example below will compile for Db2-LUW, but there are other errors and problems in your code that you will find later with testing.



        CREATE OR replace PROCEDURE log_test
        ( IN QUERY VARCHAR(24576),
        IN LOGTBL varchar(20) )
        LANGUAGE SQL
        specific log_test
        BEGIN
        DECLARE SQLCODE INTEGER DEFAULT 0;
        DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
        DECLARE v_select_query VARCHAR(24576);
        DECLARE v_query VARCHAR(24576);
        DECLARE v_logtbl varchar(20);
        DECLARE v_errormsg varchar(2048);
        DECLARE v_time TIMESTAMP;
        DECLARE v_temp_select varchar(1024);
        DECLARE v_sqlcode INTEGER;
        DECLARE v_sqlstate CHAR(5);
        DECLARE v_bezug varchar(1024);

        DECLARE stmt STATEMENT;
        DECLARE CONTINUE HANDLER FOR SQLWARNING,NOT FOUND
        SET v_sqlcode = SQLCODE;
        DECLARE EXIT HANDLER FOR SQLEXCEPTION
        SET v_sqlstate = SQLSTATE;


        set v_select_query = 'Set (?) = ('||QUERY||')';
        set v_query = 'Set (?) = ('||QUERY||')';
        set v_bezug = 'bezug';
        set v_logtbl = logtbl;
        set v_time = CURRENT TIMESTAMP;



        PREPARE stmt from v_select_query;

        EXECUTE stmt into v_temp_select;

        END@





        share|improve this answer













        As per comments, fix the syntax errors in your code. The example below will compile for Db2-LUW, but there are other errors and problems in your code that you will find later with testing.



        CREATE OR replace PROCEDURE log_test
        ( IN QUERY VARCHAR(24576),
        IN LOGTBL varchar(20) )
        LANGUAGE SQL
        specific log_test
        BEGIN
        DECLARE SQLCODE INTEGER DEFAULT 0;
        DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
        DECLARE v_select_query VARCHAR(24576);
        DECLARE v_query VARCHAR(24576);
        DECLARE v_logtbl varchar(20);
        DECLARE v_errormsg varchar(2048);
        DECLARE v_time TIMESTAMP;
        DECLARE v_temp_select varchar(1024);
        DECLARE v_sqlcode INTEGER;
        DECLARE v_sqlstate CHAR(5);
        DECLARE v_bezug varchar(1024);

        DECLARE stmt STATEMENT;
        DECLARE CONTINUE HANDLER FOR SQLWARNING,NOT FOUND
        SET v_sqlcode = SQLCODE;
        DECLARE EXIT HANDLER FOR SQLEXCEPTION
        SET v_sqlstate = SQLSTATE;


        set v_select_query = 'Set (?) = ('||QUERY||')';
        set v_query = 'Set (?) = ('||QUERY||')';
        set v_bezug = 'bezug';
        set v_logtbl = logtbl;
        set v_time = CURRENT TIMESTAMP;



        PREPARE stmt from v_select_query;

        EXECUTE stmt into v_temp_select;

        END@






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 12:49









        maomao

        4,1631421




        4,1631421
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319330%2fget-sqlcode-and-sqlstate-from-stored-procedure-with-executing-a-statement-from-v%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."