Getting SQLCODE and SQLSTATE from select query inside Procedure DB2












0















I have a procedure and all I want is the SQLSTATE and SQLCODE from a select query. I am NOT interested in the result itself.



 Create procedure select_test(IN query varchar(1024))
Language sql
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE V_QUERY VARCHAR(24576);
DECLARE V_ERRORMSG VARCHAR(2048) DEFAULT '';
DECLARE V_SQLCODE INTEGER DEFAULT 0;
DECLARE V_SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE STMT STATEMENT;
DECLARE C1 CURSOR FOR STMT;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING,NOT FOUND
BEGIN
GET DIAGNOSTICS
EXCEPTION
1 V_ERRORMSG = MESSAGE_TEXT;
VALUES(SQLSTATE,SQLCODE)INTO V_SQLSTATE, V_SQLCODE;
END;

SET V_QUERY = QUERY;
PREPARE STMT FROM V_QUERY;

OPEN C1; CLOSE C1;

End@


All I need is the SQLCODE,SQLSTATE and MESSAGE_TEXT. The result of the select is not important.
Query is something like



 SELECT A,B,C from TAB_A;


with a wrong table in the query.



Thanks to @Mark Barinstein I got hinted to the cursor part so thank you for that lready.



The problem is that I get SQLCODE -504 and it should be -204. So it is not the SQLCODE of the select.



I am running DB2 Windows v10.5



Any idea?



Thanks










share|improve this question


















  • 1





    I don't know about DB2, but it looks like Standard SQL SPL. Instead of VALUES(SQLSTATE,SQLCODE)INTO V_SQLSTATE, V_SQLCODE; try adding it to GET DIAGNOSTICS: V_ERRORMSG = MESSAGE_TEXT, V_SQLSTATE = SQLSTATE, V_SQLCODE = SQLCODE;

    – dnoeth
    Nov 16 '18 at 12:01






  • 1





    Additionally: If you don't care about the result of the Select, you don't have to open/close the cursro, prepare will already fail when the table doesn't exist...

    – dnoeth
    Nov 16 '18 at 12:06











  • the adding doesn't work in DB2

    – Viking
    Nov 16 '18 at 12:18











  • deleting the open and close worked

    – Viking
    Nov 16 '18 at 12:20






  • 1





    You use a continue handler, there's another error when you open the cursor which overrides the 1st error.

    – dnoeth
    Nov 16 '18 at 12:50
















0















I have a procedure and all I want is the SQLSTATE and SQLCODE from a select query. I am NOT interested in the result itself.



 Create procedure select_test(IN query varchar(1024))
Language sql
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE V_QUERY VARCHAR(24576);
DECLARE V_ERRORMSG VARCHAR(2048) DEFAULT '';
DECLARE V_SQLCODE INTEGER DEFAULT 0;
DECLARE V_SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE STMT STATEMENT;
DECLARE C1 CURSOR FOR STMT;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING,NOT FOUND
BEGIN
GET DIAGNOSTICS
EXCEPTION
1 V_ERRORMSG = MESSAGE_TEXT;
VALUES(SQLSTATE,SQLCODE)INTO V_SQLSTATE, V_SQLCODE;
END;

SET V_QUERY = QUERY;
PREPARE STMT FROM V_QUERY;

OPEN C1; CLOSE C1;

End@


All I need is the SQLCODE,SQLSTATE and MESSAGE_TEXT. The result of the select is not important.
Query is something like



 SELECT A,B,C from TAB_A;


with a wrong table in the query.



Thanks to @Mark Barinstein I got hinted to the cursor part so thank you for that lready.



The problem is that I get SQLCODE -504 and it should be -204. So it is not the SQLCODE of the select.



I am running DB2 Windows v10.5



Any idea?



Thanks










share|improve this question


















  • 1





    I don't know about DB2, but it looks like Standard SQL SPL. Instead of VALUES(SQLSTATE,SQLCODE)INTO V_SQLSTATE, V_SQLCODE; try adding it to GET DIAGNOSTICS: V_ERRORMSG = MESSAGE_TEXT, V_SQLSTATE = SQLSTATE, V_SQLCODE = SQLCODE;

    – dnoeth
    Nov 16 '18 at 12:01






  • 1





    Additionally: If you don't care about the result of the Select, you don't have to open/close the cursro, prepare will already fail when the table doesn't exist...

    – dnoeth
    Nov 16 '18 at 12:06











  • the adding doesn't work in DB2

    – Viking
    Nov 16 '18 at 12:18











  • deleting the open and close worked

    – Viking
    Nov 16 '18 at 12:20






  • 1





    You use a continue handler, there's another error when you open the cursor which overrides the 1st error.

    – dnoeth
    Nov 16 '18 at 12:50














0












0








0








I have a procedure and all I want is the SQLSTATE and SQLCODE from a select query. I am NOT interested in the result itself.



 Create procedure select_test(IN query varchar(1024))
Language sql
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE V_QUERY VARCHAR(24576);
DECLARE V_ERRORMSG VARCHAR(2048) DEFAULT '';
DECLARE V_SQLCODE INTEGER DEFAULT 0;
DECLARE V_SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE STMT STATEMENT;
DECLARE C1 CURSOR FOR STMT;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING,NOT FOUND
BEGIN
GET DIAGNOSTICS
EXCEPTION
1 V_ERRORMSG = MESSAGE_TEXT;
VALUES(SQLSTATE,SQLCODE)INTO V_SQLSTATE, V_SQLCODE;
END;

SET V_QUERY = QUERY;
PREPARE STMT FROM V_QUERY;

OPEN C1; CLOSE C1;

End@


All I need is the SQLCODE,SQLSTATE and MESSAGE_TEXT. The result of the select is not important.
Query is something like



 SELECT A,B,C from TAB_A;


with a wrong table in the query.



Thanks to @Mark Barinstein I got hinted to the cursor part so thank you for that lready.



The problem is that I get SQLCODE -504 and it should be -204. So it is not the SQLCODE of the select.



I am running DB2 Windows v10.5



Any idea?



Thanks










share|improve this question














I have a procedure and all I want is the SQLSTATE and SQLCODE from a select query. I am NOT interested in the result itself.



 Create procedure select_test(IN query varchar(1024))
Language sql
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE V_QUERY VARCHAR(24576);
DECLARE V_ERRORMSG VARCHAR(2048) DEFAULT '';
DECLARE V_SQLCODE INTEGER DEFAULT 0;
DECLARE V_SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE STMT STATEMENT;
DECLARE C1 CURSOR FOR STMT;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING,NOT FOUND
BEGIN
GET DIAGNOSTICS
EXCEPTION
1 V_ERRORMSG = MESSAGE_TEXT;
VALUES(SQLSTATE,SQLCODE)INTO V_SQLSTATE, V_SQLCODE;
END;

SET V_QUERY = QUERY;
PREPARE STMT FROM V_QUERY;

OPEN C1; CLOSE C1;

End@


All I need is the SQLCODE,SQLSTATE and MESSAGE_TEXT. The result of the select is not important.
Query is something like



 SELECT A,B,C from TAB_A;


with a wrong table in the query.



Thanks to @Mark Barinstein I got hinted to the cursor part so thank you for that lready.



The problem is that I get SQLCODE -504 and it should be -204. So it is not the SQLCODE of the select.



I am running DB2 Windows v10.5



Any idea?



Thanks







sql stored-procedures error-handling db2 execute






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 11:53









VikingViking

466




466








  • 1





    I don't know about DB2, but it looks like Standard SQL SPL. Instead of VALUES(SQLSTATE,SQLCODE)INTO V_SQLSTATE, V_SQLCODE; try adding it to GET DIAGNOSTICS: V_ERRORMSG = MESSAGE_TEXT, V_SQLSTATE = SQLSTATE, V_SQLCODE = SQLCODE;

    – dnoeth
    Nov 16 '18 at 12:01






  • 1





    Additionally: If you don't care about the result of the Select, you don't have to open/close the cursro, prepare will already fail when the table doesn't exist...

    – dnoeth
    Nov 16 '18 at 12:06











  • the adding doesn't work in DB2

    – Viking
    Nov 16 '18 at 12:18











  • deleting the open and close worked

    – Viking
    Nov 16 '18 at 12:20






  • 1





    You use a continue handler, there's another error when you open the cursor which overrides the 1st error.

    – dnoeth
    Nov 16 '18 at 12:50














  • 1





    I don't know about DB2, but it looks like Standard SQL SPL. Instead of VALUES(SQLSTATE,SQLCODE)INTO V_SQLSTATE, V_SQLCODE; try adding it to GET DIAGNOSTICS: V_ERRORMSG = MESSAGE_TEXT, V_SQLSTATE = SQLSTATE, V_SQLCODE = SQLCODE;

    – dnoeth
    Nov 16 '18 at 12:01






  • 1





    Additionally: If you don't care about the result of the Select, you don't have to open/close the cursro, prepare will already fail when the table doesn't exist...

    – dnoeth
    Nov 16 '18 at 12:06











  • the adding doesn't work in DB2

    – Viking
    Nov 16 '18 at 12:18











  • deleting the open and close worked

    – Viking
    Nov 16 '18 at 12:20






  • 1





    You use a continue handler, there's another error when you open the cursor which overrides the 1st error.

    – dnoeth
    Nov 16 '18 at 12:50








1




1





I don't know about DB2, but it looks like Standard SQL SPL. Instead of VALUES(SQLSTATE,SQLCODE)INTO V_SQLSTATE, V_SQLCODE; try adding it to GET DIAGNOSTICS: V_ERRORMSG = MESSAGE_TEXT, V_SQLSTATE = SQLSTATE, V_SQLCODE = SQLCODE;

– dnoeth
Nov 16 '18 at 12:01





I don't know about DB2, but it looks like Standard SQL SPL. Instead of VALUES(SQLSTATE,SQLCODE)INTO V_SQLSTATE, V_SQLCODE; try adding it to GET DIAGNOSTICS: V_ERRORMSG = MESSAGE_TEXT, V_SQLSTATE = SQLSTATE, V_SQLCODE = SQLCODE;

– dnoeth
Nov 16 '18 at 12:01




1




1





Additionally: If you don't care about the result of the Select, you don't have to open/close the cursro, prepare will already fail when the table doesn't exist...

– dnoeth
Nov 16 '18 at 12:06





Additionally: If you don't care about the result of the Select, you don't have to open/close the cursro, prepare will already fail when the table doesn't exist...

– dnoeth
Nov 16 '18 at 12:06













the adding doesn't work in DB2

– Viking
Nov 16 '18 at 12:18





the adding doesn't work in DB2

– Viking
Nov 16 '18 at 12:18













deleting the open and close worked

– Viking
Nov 16 '18 at 12:20





deleting the open and close worked

– Viking
Nov 16 '18 at 12:20




1




1





You use a continue handler, there's another error when you open the cursor which overrides the 1st error.

– dnoeth
Nov 16 '18 at 12:50





You use a continue handler, there's another error when you open the cursor which overrides the 1st error.

– dnoeth
Nov 16 '18 at 12:50












0






active

oldest

votes












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%2f53337371%2fgetting-sqlcode-and-sqlstate-from-select-query-inside-procedure-db2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53337371%2fgetting-sqlcode-and-sqlstate-from-select-query-inside-procedure-db2%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

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly