如何在执行立即语句中包含变量?
Posted
技术标签:
【中文标题】如何在执行立即语句中包含变量?【英文标题】:How to include a variable in a execute immediate statement? 【发布时间】:2021-04-08 15:54:09 【问题描述】:我试图在执行立即语句中选择一个变量,但是我不确定语法。
我试图运行的代码是这样的:
DECLARE
PREV_PAR VARCHAR2(1);
VC_NUM VARCHAR2(2);
BEGIN
SELECT 'P' INTO PREV_PAR FROM DUAL;
SELECT '01' INTO VC_NUM FROM DUAL;
EXECUTE IMMEDIATE 'CREATE TABLE TEMP_TABLE AS (SELECT '||PREV_PAR||' as prev_par,'||VC_NUM||' as vc_code from dual)';
END;
我做错了什么?
【问题讨论】:
【参考方案1】:您需要再添加两个引号来包围您的每个 varchar 变量,如下所示。
DECLARE
PREV_PAR VARCHAR2(1);
VC_NUM VARCHAR2(2);
BEGIN
SELECT 'P' INTO PREV_PAR FROM DUAL;
SELECT '01' INTO VC_NUM FROM DUAL;
EXECUTE IMMEDIATE 'CREATE TABLE TEMP_TABLE AS (SELECT '''||PREV_PAR||''' as prev_par,'''||VC_NUM||''' as vc_code from dual)';
END;
/
因为运行时的最终语句必须是这样的:
CREATE TABLE TEMP_TABLE AS (SELECT 'P' as prev_par, '01' as vc_code from dual)
【讨论】:
感谢您的帮助。以上是关于如何在执行立即语句中包含变量?的主要内容,如果未能解决你的问题,请参考以下文章