我正在尝试从现有表创建插入语句。但是,我最终遇到了ORA-00900
问题。令人惊讶的是,当我使用execute immediate
独立执行结果时,它工作正常。
让我们创建2个表:
create table a (
name varchar2 (100),
age integer
);
insert into a values ('Tom', 1);
commit;
insert into a values ('John', 2);
commit;
create table b (
name varchar2 (100),
age integer
);
insert into b values ('Jane', 1);
commit;
insert into b values ('Eric', 2);
commit;
现在这是我正在处理以获取数据的块。该块将使用SQL Plus。命令行参数将是:
SQL> @export.ddl my_schema a,b
目前,我不是在这里写SET
语句。
/* export.ddl */
declare
p_schema_name varchar2(1000) := upper('&1');
p_table_names varchar2(4000) := upper('&2');
l_statement varchar2(30000);
type tmp_table is table of varchar2(30000);
l_result tmp_table;
begin
for c_table in (
select
regexp_substr (replace(p_table_names, ' ', ''), '[^,]+', 1, level) as t_name
from dual
connect by
regexp_substr (replace(p_table_names, ' ', ''), '[^,]+', 1, level) is not null)
loop
for c_col in (
select
'''select '''||ins.insert_stmt||'('||recs.select_rows||''')'''' stmt from '||recs.owner||'.'||recs.table_name||'''' as statement
from (
select
atc.owner,
atc.table_name,
''||listagg(case when atc.data_type = 'VARCHAR2' then '''''||decode('||atc.column_name||', null, ''''null'''', ''''''''''''''''||'||atc.column_name||'||'''''''''''''''')||'''
when atc.data_type = 'NUMBER' then '''''||decode('||atc.column_name||', null, ''''null'''', '||atc.column_name||')||'''
when atc.data_type = 'DATE' then 'to_date(''''''||to_char('||atc.column_name||', ''DD-MON-YYYY HH24:MI:SS'')||'''''', ''''DD-MON-YYYY HH24:MI:SS'''')'
end, ''', ') within group (order by atc.column_id) as select_rows
from dba_tab_columns atc
where atc.table_name in (c_table.t_name) -- here
group by atc.owner, atc.table_name) recs,
(
select
atc.owner,
atc.table_name,
'''insert into '||atc.owner||'.'||atc.table_name||' ('||listagg(atc.column_name, ', ') within group (order by atc.column_id)||') VALUES ' as insert_stmt
from all_tab_columns atc
where atc.table_name in (c_table.t_name) -- here
group by atc.owner, atc.table_name
) ins
where recs.owner = ins.owner
and recs.table_name = ins.table_name)
loop
l_statement := c_col.statement;
dbms_output.put_line(l_statement);
execute immediate l_statement bulk collect into l_result;
for i in 1 .. l_result.count() loop
dbms_output.put_line(l_result(i));
end loop;
end loop;
end loop;
end;
在这段代码中,我面对:
ORA-00900: invalid SQL statement
ORA-06512: at line 44
这是执行立即声明的行号。
但是,如果我只复制dbms_output(注释掉执行立即语句和跟随循环),那就是:
'select ''insert into MY_SCHEMA.A (NAME, AGE) VALUES (''||decode(NAME, null, ''null'', ''''''''||NAME||'''''''')||'', ''||decode(AGE, null, ''null'', AGE)||'')'' stmt from MY_SCHEMA.A'
'select ''insert into MY_SCHEMA.B (NAME, AGE) VALUES (''||decode(NAME, null, ''null'', ''''''''||NAME||'''''''')||'', ''||decode(AGE, null, ''null'', AGE)||'')'' stmt from MY_SCHEMA.B'
并使用它:
/* second.ddl */
declare
l_statememt varchar2(30000);
type typ_varcharlist IS TABLE OF VARCHAR2(30000);
col_list typ_varcharlist;
statemement varchar2(30000) := 'select ''insert into MY_SCHEMA.A (NAME, AGE) VALUES (''||decode(NAME, null, ''null'', ''''''''||NAME||'''''''')||'', ''||decode(AGE, null, ''null'', AGE)||'')'' stmt from MY_SCHEMA.A';
begin
execute immediate statemement bulk collect into col_list;
for i in 1 .. col_list.count() loop
dbms_output.put_line(col_list(i));
end loop;
end;
然后我得到了所需的输出:
insert into MY_SCHEMA.A (NAME, AGE) VALUES ('Tom', 1)
insert into MY_SCHEMA.A (NAME, AGE) VALUES ('John', 2)
我的问题是: 为什么执行立即在第一个代码(export.ddl)中给我错误?我该怎么做才能阻止它,并从第二步(second.ddl)中保存自己?
这意味着,使用一个脚本来获取insert语句,例如insert.ddl script。
提前致谢: - )
答案 0 :(得分:2)
你输入太多(转义)引号。您从dbms_output
看到的生成的语句是
'select ''insert into MY_SCHEMA.A (NAME, AGE) VALUES (''||decode(NAME, null, ''null'', ''''''''||NAME||'''''''')||'', ''||decode(AGE, null, ''null'', AGE)||'')'' stmt from MY_SCHEMA.A'
当您在第二个块中运行它时,您将其作为字符串文字处理,当您将其分配给statemement
变量时 - 该分配有效地取消所有转义引号。当您在第一个块中直接运行它时,开始和结束引号都不是有效的。或者换句话说,复制输出(包括那些引号)并将其作为SQL语句直接粘贴到SQL * Plus中 - 然后将看到ORA-00900。
删除最外面的那些是不够的;只有那些被删除,i.er。改变
'''select '''||ins.insert_stmt||'('||recs.select_rows||''')'''' stmt from '||recs.owner||'.'||recs.table_name||'''' as statement
到
'select '''||ins.insert_stmt||'('||recs.select_rows||''')'' stmt from '||recs.owner||'.'||recs.table_name||'' as statement
...它得到" ORA-00923:找不到FROM关键字"。要摆脱这种情况,您需要从insert_smt
中删除多余的引号,以及何时在stmt
之前关闭:
'select '''||ins.insert_stmt||'('||recs.select_rows||''')'' stmt from '||recs.owner||'.'||recs.table_name||'' as statement
和
'insert into '||atc.owner||'.'||atc.table_name||' ('||listagg(atc.column_name, ', ') within group (order by atc.column_id)||') VALUES ' as insert_stmt
...现在将运行,但不会产生您想要的输出:
PL/SQL procedure successfully completed.
select 'insert into MYSCHEMA.A (NAME, AGE) VALUES (''||decode(NAME, null, ''null'', ''''''''||NAME||'''''''')||'', ''||decode(AGE, null, ''null'', AGE)||'')' stmt from MYSCHEMA.A
insert into MYSCHEMA.A (NAME, AGE) VALUES ('||decode(NAME, null, 'null', ''''||NAME||'''')||', '||decode(AGE, null, 'null', AGE)||')
insert into MYSCHEMA.A (NAME, AGE) VALUES ('||decode(NAME, null, 'null', ''''||NAME||'''')||', '||decode(AGE, null, 'null', AGE)||')
select 'insert into MYSCHEMA.B (NAME, AGE) VALUES (''||decode(NAME, null, ''null'', ''''''''||NAME||'''''''')||'', ''||decode(AGE, null, ''null'', AGE)||'')' stmt from STACKOVERFLOW.B
insert into MYSCHEMA.B (NAME, AGE) VALUES ('||decode(NAME, null, 'null', ''''||NAME||'''')||', '||decode(AGE, null, 'null', AGE)||')
insert into MYSCHEMA.B (NAME, AGE) VALUES ('||decode(NAME, null, 'null', ''''||NAME||'''')||', '||decode(AGE, null, 'null', AGE)||')
您还需要删除很多其他转义引号。
答案 1 :(得分:1)
如果您只想生成插入语句(不完全是这样),在SQLDeveloper中有一个函数,它生成插入语句。您只需左键单击表格>导出,然后取消选中" EXPORT DDL"。下一个>接下来,你有纯SQL插入语句。