我有
l_tab1 table1%rowtype;
l_tab2 table2%rowtype;
table1和table2的结构是相同的。
如何将数据从l_tab1移动到l_tab2?
现在我可以看到两种方式,但我不喜欢它,因为我需要硬编码字段。
l_tab2.field1 := l_tab1.field1;
l_tab2.field2 := l_tab1.field2;
select * into l_tab2
from table1
where field1 = l_tab1.field1
and field2 = l_tab1.field2;
我相信它应该更容易
insert into l_tab2
values l_tab1;
或者类似的东西,不使用字段。
如果两个表具有相同的结构,则应该使用简单的赋值,至少从Oracle 11.2开始。
使用如下表格
create table table1(col1 number, col2 number);
create table table2(col1 number, col2 number);
insert into table1 values (1, 11);
insert into table2 values (2, 22);
我们有:
SQL> select * from table1;
COL1 COL2
---------- ----------
1 11
SQL> select * from table2;
COL1 COL2
---------- ----------
2 22
SQL> declare
2 l_tab1 table1%rowtype;
3 l_tab2 table2%rowtype;
4 begin
5 select *
6 into l_tab1
7 from table1;
8 l_tab2 := l_tab1;
9 insert into table2 values l_tab2;
10 end;
11 /
PL/SQL procedure successfully completed.
SQL> select * from table2;
COL1 COL2
---------- ----------
1 11
2 22
SQL>
没有指定每一列,没有办法做你想做的事。
解决方法可能是使用集合而不是记录类型。每个集合可能只有1条记录,但您可以在不指定每列的情况下执行此操作。
这个答案有一个更复杂的例子,但大致是你需要的。