如何在插入语句中使用每个月的动态 table 而我们在一列中传递一个静态值
How to use dynamic table of every month in the insert statement while one static value we are passing in one column
在一个存储过程中,我使用 insert into
语句将一个变量声明为 v_tbl_name,这是动态的。当我们在一列中传递一个静态值但出现错误时,我将在 insert into
块中使用此动态 table。这是示例代码-
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table name
v_sql varchar2(10000);
begin
v_tbl_name := v_gen_tbl || '_' || TO_CHAR(SYSDATE, 'MON');
v_sql := 'insert into tbl --- another table going to insert
(select ''abc'' as col1, ........
from '||v_tbl_name||');'
execute immediate v_sql;
commit;
end;
这里 'abc'
静态值作为列。执行上面的代码后,它会生成一个错误,如 Encountered the symbol "Execute"
.
这样做:
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table name
v_sql varchar2(1000);
begin
v_tbl_name := v_gen_tbl || '_' || TO_CHAR(SYSDATE, 'MON');
v_sql:= 'insert into tbl --- another table going to insert
select 'abc' as col1, ........
from '||v_tbl_name;
execute immediate v_sql;
commit;
end;
演示:
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table
v_sql varchar2(1000);
begin
v_tbl_name := 'DEPT';
v_sql :='insert into tbl select * from '||v_tbl_name;
execute immediate v_sql;
end;
Exec:
SQL> select * from tbl;
DEPTNO DNAME LOC
---------- -------------------- --------------------
1 XXX YYY
编辑:
Here 'abc' static value as column so that's why not able to execute
the stored proc. It is generating an err like Encountered the symbol '
abc '.
请看下面的演示:
Table tbl
正在使用来自 select 语句的静态值 abc
填充:
SQL> select * from tbl;
DEPTNO DNAME LOC B
---------- -------------------- -------------------- ---
执行后:
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table
v_sql varchar2(1000);
begin
v_tbl_name := 'DEPT';
v_sql :='insert into tbl (deptno,dname,loc,b) select a.*, ''abc'' from '||v_tbl_name||' a';
execute immediate v_sql;
Commit;
end;
输出
SQL> SELECT * FROM TBL;
DEPTNO DNAME LOC B
---------- -------------------- -------------------- ---
100 Executive USA abc
最终编辑:
您没有按照我向您展示的内容进行操作,因此您陷入了问题。按照我的内联评论。请替换提到的列名称,它会得到解决。
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table name
v_sql varchar2(10000);
begin
v_tbl_name := v_gen_tbl || '_' || TO_CHAR(SYSDATE, 'MON');
v_sql := 'insert into tbl(col1,.....<all columns>) --- another table going to insert
select ''abc'' as col1, a.col2........<other columns a.<columns>
from '||v_tbl_name||' a';
execute immediate v_sql;
commit;
end;
在一个存储过程中,我使用 insert into
语句将一个变量声明为 v_tbl_name,这是动态的。当我们在一列中传递一个静态值但出现错误时,我将在 insert into
块中使用此动态 table。这是示例代码-
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table name
v_sql varchar2(10000);
begin
v_tbl_name := v_gen_tbl || '_' || TO_CHAR(SYSDATE, 'MON');
v_sql := 'insert into tbl --- another table going to insert
(select ''abc'' as col1, ........
from '||v_tbl_name||');'
execute immediate v_sql;
commit;
end;
这里 'abc'
静态值作为列。执行上面的代码后,它会生成一个错误,如 Encountered the symbol "Execute"
.
这样做:
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table name
v_sql varchar2(1000);
begin
v_tbl_name := v_gen_tbl || '_' || TO_CHAR(SYSDATE, 'MON');
v_sql:= 'insert into tbl --- another table going to insert
select 'abc' as col1, ........
from '||v_tbl_name;
execute immediate v_sql;
commit;
end;
演示:
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table
v_sql varchar2(1000);
begin
v_tbl_name := 'DEPT';
v_sql :='insert into tbl select * from '||v_tbl_name;
execute immediate v_sql;
end;
Exec:
SQL> select * from tbl;
DEPTNO DNAME LOC
---------- -------------------- --------------------
1 XXX YYY
编辑:
Here 'abc' static value as column so that's why not able to execute the stored proc. It is generating an err like Encountered the symbol ' abc '.
请看下面的演示:
Table tbl
正在使用来自 select 语句的静态值 abc
填充:
SQL> select * from tbl;
DEPTNO DNAME LOC B
---------- -------------------- -------------------- ---
执行后:
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table
v_sql varchar2(1000);
begin
v_tbl_name := 'DEPT';
v_sql :='insert into tbl (deptno,dname,loc,b) select a.*, ''abc'' from '||v_tbl_name||' a';
execute immediate v_sql;
Commit;
end;
输出
SQL> SELECT * FROM TBL;
DEPTNO DNAME LOC B
---------- -------------------- -------------------- ---
100 Executive USA abc
最终编辑:
您没有按照我向您展示的内容进行操作,因此您陷入了问题。按照我的内联评论。请替换提到的列名称,它会得到解决。
declare
v_tbl_name varchar2(5) := NULL;
v_gen_tbl constant varchar2(50) := 'arch_tbl_mon'; --table name
v_sql varchar2(10000);
begin
v_tbl_name := v_gen_tbl || '_' || TO_CHAR(SYSDATE, 'MON');
v_sql := 'insert into tbl(col1,.....<all columns>) --- another table going to insert
select ''abc'' as col1, a.col2........<other columns a.<columns>
from '||v_tbl_name||' a';
execute immediate v_sql;
commit;
end;