如果你的表結構定義列的缺省值,您也可以考慮有條件插入:
insert all
when :p_nr = 1 then into A(col1) values(p_value)
when :p_nr = 2 then into A(col2) values(p_value)
when :p_nr = 3 then into A(col3) values(p_value)
select :p_value as p_value from dual
;
的優點是,這個查詢榮譽默認值,看一個例子:
create table A(
col1 varchar2(10) default 'default 1',
col2 varchar2(10) default 'default 2',
col3 varchar2(10) default 'default 3'
);
variable p_nr number
variable p_value varchar2(100)
exec :p_nr:=2
exec :p_value:='value'
insert into A (col1, col2, col3)
values (case when :p_nr = 1 then :p_value end,
case when :p_nr = 2 then :p_value end,
case when :p_nr = 3 then :p_value end);
select * from A;
COL1 COL2 COL3
---------- ---------- ----------
value
和:
rollback;
insert all
when :p_nr = 1 then into A(col1) values(p_value)
when :p_nr = 2 then into A(col2) values(p_value)
when :p_nr = 3 then into A(col3) values(p_value)
select :p_value as p_value from dual
;
select * from A;
COL1 COL2 COL3
---------- ---------- ----------
default 1 value default 3