Insert multiple rows of data with one insert statement

oracle, sqlserver do not support insert into t (x) values ​​(), ();
Only mysql supports insert into t(x) values(),();

insert all encounters sequence
When insert all encounters sequence
In the past few days, when modifying the relevant PL/SQL package code in Business Letter 2.0, when implementing such an SQL:
insert all into bo(object_name,sn) values(object_name,seqkey)
                into co(object_name,sn)  values(object_name,seqkey)
select object_name ,sq1.nextval from ao
ORACLE reported ORA-02287 error, the reason is that INSERT ALL needs to insert multiple tables, but the SEQUENCE in the SELECT part does not allow SELECT multiple values ​​at a time. There are two solutions:
Method 1: Write a function, write the process of obtaining sq1.nextval in the function, and then refer to the function in the select section, as follows:
create or replace function f_getseqkey return number is
result number;
begin
select sq1.nextval into result from dual ;
return result ;
end;

SQL:insert all into bo(object_name,sn) values(object_name,seqkey)
                        into co(object_name,sn) values(object_name,seqkey-1)
select object_name ,f_getseqkey() seqkey from ao;
Description: There is a behavior of -1 here. The research found that when inserting multiple tables, the subsequent SELECT actually performs the NEXTVAL behavior twice, and in order to ensure that two INTOs insert the same sequence value, artificially perform -1 For insertion, the consequence of this solution is that sequence values ​​will be used more often. In the case of inserting more tables, it may cause the application key value to be tense. In addition, additional functions are called, and the performance will also be degraded.
Method Two:
SQL:insert all into bo(object_name,sn) values(object_name,sq1.nextval)
                          into co(object_name,sn) values(object_name,sq1.nextval)
          select object_name from ao
This method is the best, it does not need to define functions, does not use sequences, and has good performance.
Phenomenon: NEXTVAL is used in both INTOs, but is it guaranteed to be the same value? The answer is OK, because INSERT ALL to multiple tables is the same SQL in ORACLE's view, that is, similar: SQL such as SELECT SEQ.NEXTVAL, SEQ.NEXTVAL FROM DUAL, the same sequence is called multiple times in the same SQL, and the value is the same. But if you call this custom function multiple times in the same SQL, you will get different values.


Part from: http://blog.csdn.net/xuxurui007/article/details/7692065

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326939785&siteId=291194637