使用连接的命令字符串在过程中执行sql

时间:2018-11-15 15:21:01

标签: sql stored-procedures insert db2 execute

我在这里:

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@

问题在于它似乎正在运行(没有错误消息),但是表中没有任何内容。...

有什么想法吗? 谢谢

2 个答案:

答案 0 :(得分:1)

似乎您没有使用调试器...请尝试在IBM Data Studio中使用调试器(免费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@

答案 1 :(得分:1)

我认为您需要将测试更改为“ ||测试||”在如下所示的v_query集合中:

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@