db2—执行存储在表列中的sql语句

ha5z0ras  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(280)

我在db2数据库表列中有insert语句,我想执行那个in-store过程
表格:

TB_SQL
(
DATA_SQL VARCHAR(12000)
)

data\ U sql列包含insert语句。例如:
从tb\u sql中选择*

INSERT INTO ADDRESS (COL1, COL2) 
SELECT COL1, COL2 FROM DEMO WHERE TYPE='ADDRESS'

如何准备和执行db2存储过程中data\ U sql列的insert语句?
我试过的代码:

SET v_SQL=
        'SET ?=(
                    SELECT 
                    DATA_SQL
                    FROM TB_SQL
                    WHERE TBNAME='''||v_TBNAME||'''
               )
        ';
        PREPARE SQL_QUERY FROM v_SQL;
        EXECUTE SQL_QUERY ;

实际上,上面的prepare和execute只会准备select语句和执行select语句。我想要的是,我想要执行select语句的输出语句。
我还有下面的选项将insert语句再次存储在另一个变量中

SET v_SQL=
        'SET ?=(
                    SELECT 
                    DATA_SQL
                    FROM TB_SQL
                    WHERE TBNAME='''||v_TBNAME||'''
               )
        ';
        PREPARE SQL_QUERY FROM v_SQL;
        EXECUTE SQL_QUERY INTO v_INSERT_STATEMENT;
        PREPARE SQL_INSERT FROM v_INSERT_STATEMENT;
        EXECUTE SQL_INSERT;

我相信现在我有insert语句存储在 v_INSERT_STATEMENT 再次从变量中准备sql,然后执行。但这是行不通的。

dxxyhpgq

dxxyhpgq1#

你不需要两个动态语句…一个静态语句和一个动态语句更有意义。

select data_sql into v_Insert_statement
from tb_sql 
where tbname = v_TBNAME;

PREPARE SQL_INSERT FROM v_INSERT_STATEMENT;
EXECUTE SQL_INSERT;
4xrmg8kj

4xrmg8kj2#

SET v_SQL=
        'SET ?=(
                    SELECT 
                    DATA_SQL
                    FROM TB_SQL
                    WHERE TBNAME='''||v_TBNAME||'''
               )
        ';
        PREPARE SQL_QUERY FROM v_SQL;
        EXECUTE SQL_QUERY INTO v_INSERT_STATEMENT;
        PREPARE SQL_INSERT FROM v_INSERT_STATEMENT;
        EXECUTE SQL_INSERT;

相关问题