Executing sql in procedure with concatenated command string










0















I have this here:



create procedure test_ins(IN tab varchar(128))
Language sql
begin
DECLARE stmt_ins STATEMENT;
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values (test,'''')';
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@


The problem is that it seems to run (no error message) but there is nothing in the table....



Any ideas?
Thanks










share|improve this question
























  • can you try: set v_query= 'INSERT INTO '||test_tab||'(test, free) values ('||test||','''')';

    – Eray Balkanli
    Nov 15 '18 at 15:24











  • Or, if my comment above does not work: set v_query= 'INSERT INTO '||test_tab||'(test, free) values (''test'','''')';

    – Eray Balkanli
    Nov 15 '18 at 15:25












  • both don't work

    – Viking
    Nov 15 '18 at 15:34











  • well, at least we have fixed a potential future problem. Can you also try "EXEC SQL EXECUTE stmt_ins" at the end.?

    – Eray Balkanli
    Nov 15 '18 at 15:42






  • 1





    Oh found it, we are forgetting single quotes, should be like 'Here it is', which is '''||test||''' . Updated my answer, check that.

    – Eray Balkanli
    Nov 15 '18 at 16:00















0















I have this here:



create procedure test_ins(IN tab varchar(128))
Language sql
begin
DECLARE stmt_ins STATEMENT;
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values (test,'''')';
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@


The problem is that it seems to run (no error message) but there is nothing in the table....



Any ideas?
Thanks










share|improve this question
























  • can you try: set v_query= 'INSERT INTO '||test_tab||'(test, free) values ('||test||','''')';

    – Eray Balkanli
    Nov 15 '18 at 15:24











  • Or, if my comment above does not work: set v_query= 'INSERT INTO '||test_tab||'(test, free) values (''test'','''')';

    – Eray Balkanli
    Nov 15 '18 at 15:25












  • both don't work

    – Viking
    Nov 15 '18 at 15:34











  • well, at least we have fixed a potential future problem. Can you also try "EXEC SQL EXECUTE stmt_ins" at the end.?

    – Eray Balkanli
    Nov 15 '18 at 15:42






  • 1





    Oh found it, we are forgetting single quotes, should be like 'Here it is', which is '''||test||''' . Updated my answer, check that.

    – Eray Balkanli
    Nov 15 '18 at 16:00













0












0








0








I have this here:



create procedure test_ins(IN tab varchar(128))
Language sql
begin
DECLARE stmt_ins STATEMENT;
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values (test,'''')';
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@


The problem is that it seems to run (no error message) but there is nothing in the table....



Any ideas?
Thanks










share|improve this question
















I have this here:



create procedure test_ins(IN tab varchar(128))
Language sql
begin
DECLARE stmt_ins STATEMENT;
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values (test,'''')';
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@


The problem is that it seems to run (no error message) but there is nothing in the table....



Any ideas?
Thanks







sql stored-procedures insert db2 execute






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 16:49









marc_s

582k13011221269




582k13011221269










asked Nov 15 '18 at 15:21









VikingViking

466




466












  • can you try: set v_query= 'INSERT INTO '||test_tab||'(test, free) values ('||test||','''')';

    – Eray Balkanli
    Nov 15 '18 at 15:24











  • Or, if my comment above does not work: set v_query= 'INSERT INTO '||test_tab||'(test, free) values (''test'','''')';

    – Eray Balkanli
    Nov 15 '18 at 15:25












  • both don't work

    – Viking
    Nov 15 '18 at 15:34











  • well, at least we have fixed a potential future problem. Can you also try "EXEC SQL EXECUTE stmt_ins" at the end.?

    – Eray Balkanli
    Nov 15 '18 at 15:42






  • 1





    Oh found it, we are forgetting single quotes, should be like 'Here it is', which is '''||test||''' . Updated my answer, check that.

    – Eray Balkanli
    Nov 15 '18 at 16:00

















  • can you try: set v_query= 'INSERT INTO '||test_tab||'(test, free) values ('||test||','''')';

    – Eray Balkanli
    Nov 15 '18 at 15:24











  • Or, if my comment above does not work: set v_query= 'INSERT INTO '||test_tab||'(test, free) values (''test'','''')';

    – Eray Balkanli
    Nov 15 '18 at 15:25












  • both don't work

    – Viking
    Nov 15 '18 at 15:34











  • well, at least we have fixed a potential future problem. Can you also try "EXEC SQL EXECUTE stmt_ins" at the end.?

    – Eray Balkanli
    Nov 15 '18 at 15:42






  • 1





    Oh found it, we are forgetting single quotes, should be like 'Here it is', which is '''||test||''' . Updated my answer, check that.

    – Eray Balkanli
    Nov 15 '18 at 16:00
















can you try: set v_query= 'INSERT INTO '||test_tab||'(test, free) values ('||test||','''')';

– Eray Balkanli
Nov 15 '18 at 15:24





can you try: set v_query= 'INSERT INTO '||test_tab||'(test, free) values ('||test||','''')';

– Eray Balkanli
Nov 15 '18 at 15:24













Or, if my comment above does not work: set v_query= 'INSERT INTO '||test_tab||'(test, free) values (''test'','''')';

– Eray Balkanli
Nov 15 '18 at 15:25






Or, if my comment above does not work: set v_query= 'INSERT INTO '||test_tab||'(test, free) values (''test'','''')';

– Eray Balkanli
Nov 15 '18 at 15:25














both don't work

– Viking
Nov 15 '18 at 15:34





both don't work

– Viking
Nov 15 '18 at 15:34













well, at least we have fixed a potential future problem. Can you also try "EXEC SQL EXECUTE stmt_ins" at the end.?

– Eray Balkanli
Nov 15 '18 at 15:42





well, at least we have fixed a potential future problem. Can you also try "EXEC SQL EXECUTE stmt_ins" at the end.?

– Eray Balkanli
Nov 15 '18 at 15:42




1




1





Oh found it, we are forgetting single quotes, should be like 'Here it is', which is '''||test||''' . Updated my answer, check that.

– Eray Balkanli
Nov 15 '18 at 16:00





Oh found it, we are forgetting single quotes, should be like 'Here it is', which is '''||test||''' . Updated my answer, check that.

– Eray Balkanli
Nov 15 '18 at 16:00












2 Answers
2






active

oldest

votes


















1














I think you need to change test to '||test||' in set v_query like below:



create procedure test_ins(IN tab varchar(128))
Language sql
begin
DECLARE stmt_ins STATEMENT;
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values ( '''||test||''','''')';
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@





share|improve this answer

























  • Yeah found that in the end too. Thanks for your help!!!

    – Viking
    Nov 15 '18 at 16:40


















1














Seems you are not using a debugger...try the one in IBM Data Studio (free download).



create or replace procedure test_ins(IN tab varchar(128))
Language sql
specific test_ins
begin
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
DECLARE stmt_ins STATEMENT;
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values (''test'','''')';
call dbms_output.put_line(v_query);
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@





share|improve this answer























  • Debugger doesn't start (user-id does not have priviledges) (I'm admin on the machine), however it doesn't work. nothing in the log table and no errors

    – Viking
    Nov 15 '18 at 15:33






  • 1





    Debugger works fine for me, as local-admin, so you've got a different issue not related to your question. The exact code I show in my answer works correctly. Your code may differ. If the exact code above yields neither and insert nor an error, then you are omitting some information.

    – mao
    Nov 15 '18 at 15:37











  • When I just execute the statement directly it works with prepare and execute it doesn't

    – Viking
    Nov 15 '18 at 15:39











  • Does the exact code shown in my answer work on your environment? It does not help you if you change the question inside a comment!

    – mao
    Nov 15 '18 at 15:41











  • No it doesn't sadly

    – Viking
    Nov 15 '18 at 15:42










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%2f53322604%2fexecuting-sql-in-procedure-with-concatenated-command-string%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I think you need to change test to '||test||' in set v_query like below:



create procedure test_ins(IN tab varchar(128))
Language sql
begin
DECLARE stmt_ins STATEMENT;
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values ( '''||test||''','''')';
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@





share|improve this answer

























  • Yeah found that in the end too. Thanks for your help!!!

    – Viking
    Nov 15 '18 at 16:40















1














I think you need to change test to '||test||' in set v_query like below:



create procedure test_ins(IN tab varchar(128))
Language sql
begin
DECLARE stmt_ins STATEMENT;
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values ( '''||test||''','''')';
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@





share|improve this answer

























  • Yeah found that in the end too. Thanks for your help!!!

    – Viking
    Nov 15 '18 at 16:40













1












1








1







I think you need to change test to '||test||' in set v_query like below:



create procedure test_ins(IN tab varchar(128))
Language sql
begin
DECLARE stmt_ins STATEMENT;
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values ( '''||test||''','''')';
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@





share|improve this answer















I think you need to change test to '||test||' in set v_query like below:



create procedure test_ins(IN tab varchar(128))
Language sql
begin
DECLARE stmt_ins STATEMENT;
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values ( '''||test||''','''')';
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 15:59

























answered Nov 15 '18 at 15:31









Eray BalkanliEray Balkanli

4,47452346




4,47452346












  • Yeah found that in the end too. Thanks for your help!!!

    – Viking
    Nov 15 '18 at 16:40

















  • Yeah found that in the end too. Thanks for your help!!!

    – Viking
    Nov 15 '18 at 16:40
















Yeah found that in the end too. Thanks for your help!!!

– Viking
Nov 15 '18 at 16:40





Yeah found that in the end too. Thanks for your help!!!

– Viking
Nov 15 '18 at 16:40













1














Seems you are not using a debugger...try the one in IBM Data Studio (free download).



create or replace procedure test_ins(IN tab varchar(128))
Language sql
specific test_ins
begin
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
DECLARE stmt_ins STATEMENT;
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values (''test'','''')';
call dbms_output.put_line(v_query);
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@





share|improve this answer























  • Debugger doesn't start (user-id does not have priviledges) (I'm admin on the machine), however it doesn't work. nothing in the log table and no errors

    – Viking
    Nov 15 '18 at 15:33






  • 1





    Debugger works fine for me, as local-admin, so you've got a different issue not related to your question. The exact code I show in my answer works correctly. Your code may differ. If the exact code above yields neither and insert nor an error, then you are omitting some information.

    – mao
    Nov 15 '18 at 15:37











  • When I just execute the statement directly it works with prepare and execute it doesn't

    – Viking
    Nov 15 '18 at 15:39











  • Does the exact code shown in my answer work on your environment? It does not help you if you change the question inside a comment!

    – mao
    Nov 15 '18 at 15:41











  • No it doesn't sadly

    – Viking
    Nov 15 '18 at 15:42















1














Seems you are not using a debugger...try the one in IBM Data Studio (free download).



create or replace procedure test_ins(IN tab varchar(128))
Language sql
specific test_ins
begin
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
DECLARE stmt_ins STATEMENT;
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values (''test'','''')';
call dbms_output.put_line(v_query);
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@





share|improve this answer























  • Debugger doesn't start (user-id does not have priviledges) (I'm admin on the machine), however it doesn't work. nothing in the log table and no errors

    – Viking
    Nov 15 '18 at 15:33






  • 1





    Debugger works fine for me, as local-admin, so you've got a different issue not related to your question. The exact code I show in my answer works correctly. Your code may differ. If the exact code above yields neither and insert nor an error, then you are omitting some information.

    – mao
    Nov 15 '18 at 15:37











  • When I just execute the statement directly it works with prepare and execute it doesn't

    – Viking
    Nov 15 '18 at 15:39











  • Does the exact code shown in my answer work on your environment? It does not help you if you change the question inside a comment!

    – mao
    Nov 15 '18 at 15:41











  • No it doesn't sadly

    – Viking
    Nov 15 '18 at 15:42













1












1








1







Seems you are not using a debugger...try the one in IBM Data Studio (free download).



create or replace procedure test_ins(IN tab varchar(128))
Language sql
specific test_ins
begin
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
DECLARE stmt_ins STATEMENT;
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values (''test'','''')';
call dbms_output.put_line(v_query);
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@





share|improve this answer













Seems you are not using a debugger...try the one in IBM Data Studio (free download).



create or replace procedure test_ins(IN tab varchar(128))
Language sql
specific test_ins
begin
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
DECLARE stmt_ins STATEMENT;
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values (''test'','''')';
call dbms_output.put_line(v_query);
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;

end@






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 15:29









maomao

4,2131421




4,2131421












  • Debugger doesn't start (user-id does not have priviledges) (I'm admin on the machine), however it doesn't work. nothing in the log table and no errors

    – Viking
    Nov 15 '18 at 15:33






  • 1





    Debugger works fine for me, as local-admin, so you've got a different issue not related to your question. The exact code I show in my answer works correctly. Your code may differ. If the exact code above yields neither and insert nor an error, then you are omitting some information.

    – mao
    Nov 15 '18 at 15:37











  • When I just execute the statement directly it works with prepare and execute it doesn't

    – Viking
    Nov 15 '18 at 15:39











  • Does the exact code shown in my answer work on your environment? It does not help you if you change the question inside a comment!

    – mao
    Nov 15 '18 at 15:41











  • No it doesn't sadly

    – Viking
    Nov 15 '18 at 15:42

















  • Debugger doesn't start (user-id does not have priviledges) (I'm admin on the machine), however it doesn't work. nothing in the log table and no errors

    – Viking
    Nov 15 '18 at 15:33






  • 1





    Debugger works fine for me, as local-admin, so you've got a different issue not related to your question. The exact code I show in my answer works correctly. Your code may differ. If the exact code above yields neither and insert nor an error, then you are omitting some information.

    – mao
    Nov 15 '18 at 15:37











  • When I just execute the statement directly it works with prepare and execute it doesn't

    – Viking
    Nov 15 '18 at 15:39











  • Does the exact code shown in my answer work on your environment? It does not help you if you change the question inside a comment!

    – mao
    Nov 15 '18 at 15:41











  • No it doesn't sadly

    – Viking
    Nov 15 '18 at 15:42
















Debugger doesn't start (user-id does not have priviledges) (I'm admin on the machine), however it doesn't work. nothing in the log table and no errors

– Viking
Nov 15 '18 at 15:33





Debugger doesn't start (user-id does not have priviledges) (I'm admin on the machine), however it doesn't work. nothing in the log table and no errors

– Viking
Nov 15 '18 at 15:33




1




1





Debugger works fine for me, as local-admin, so you've got a different issue not related to your question. The exact code I show in my answer works correctly. Your code may differ. If the exact code above yields neither and insert nor an error, then you are omitting some information.

– mao
Nov 15 '18 at 15:37





Debugger works fine for me, as local-admin, so you've got a different issue not related to your question. The exact code I show in my answer works correctly. Your code may differ. If the exact code above yields neither and insert nor an error, then you are omitting some information.

– mao
Nov 15 '18 at 15:37













When I just execute the statement directly it works with prepare and execute it doesn't

– Viking
Nov 15 '18 at 15:39





When I just execute the statement directly it works with prepare and execute it doesn't

– Viking
Nov 15 '18 at 15:39













Does the exact code shown in my answer work on your environment? It does not help you if you change the question inside a comment!

– mao
Nov 15 '18 at 15:41





Does the exact code shown in my answer work on your environment? It does not help you if you change the question inside a comment!

– mao
Nov 15 '18 at 15:41













No it doesn't sadly

– Viking
Nov 15 '18 at 15:42





No it doesn't sadly

– Viking
Nov 15 '18 at 15:42

















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%2f53322604%2fexecuting-sql-in-procedure-with-concatenated-command-string%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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto