ibatis 批量插入oracle總結

來源:互聯網
上載者:User

標籤:upd   簡單   rac   rate   擷取   測試   acl   rom   ati   

1. 使用批量插入最先想到如下的插入語句

insert into a (id,name) values(‘‘,‘‘) ,(‘‘,‘‘) 但是 oracle 並不支援這種寫法 

然後查詢得知可以使用如下寫法 

insert all 

into a(id,name) values(‘‘,‘‘) 

into a(id,name) values(‘‘,‘‘) (註:可以一次插入一個表也可以多個表)

但是這種方式有一種限制,就是 行數乘以列數不能大於1000(我沒有驗證),而且這種方式據說效率不高(我沒有驗證)

但是我做批量插入的資料時從別的介面擷取的,我無法知道到底有多少資料,而且表的資料庫超過50個,所以我捨棄了這種方式,pass.

繼續百度,獲知可以使用視圖插入,沒有行數限制(據說資料最大64k,待項目上線驗證),而且效率較高

如下:

insert into a (id,name)

<iterate conjunction="UNION ALL"  property="d">

select #d[].id#,#d[].name# from dual

</iterate>

可以成功,happy..but

我們用的是oracle ,id 是用序列產生的,簡單 ,把id 替換成序列產生就是了

於是 

insert into a (id,name)

<iterate conjunction="UNION ALL"  property="d">

select SEQ_A.nextval,#d[].name# from dual

</iterate>

運行,我去,報錯: 此處不允許序號,,什嗎???

測試下到底是什麼不支援序號,開啟plsql 

insert into a (id,name)

select SEQ_A.nextval,null from dual 

沒有問題。。。

然後執行

select SEQ_A.nextval,null from dual

union all 

select SEQ_A.nextval,null from dual

提示此處不允許序號,,問題找到了,原來是 union all 和 nextval 不能一塊用。。。

百度下原因:

Restrictions on Sequence Values You cannot use CURRVAL and NEXTVAL in the

following constructs:
■ A subquery in a DELETE, SELECT, or UPDATE statement
■ A query of a view or of a materialized view
■ A SELECT statement with the DISTINCT operator
■ A SELECT statement with a GROUP BY clause or ORDER BY clause   
■ A SELECT statement that is combined with another SELECT statement with the
UNION, INTERSECT, or MINUS set operator
■ The WHERE clause of a SELECT statement
■ The DEFAULT value of a column in a CREATE TABLE or ALTER TABLE statement
■ The condition of a CHECK constrain

參考(串連沒法放上去,因為提示內容違禁 http://www.+++ w2bc +++.com/Article/18614, 訪問時把加號去掉)

應該是上面標紅的那條導致的,大致意思是 用union 串連的兩個select 不能使用序列

但是我們看到在單個select 是可以使用的,

select Seq_a.nextval , null from dual 是正確的 。。

dual 可以是任何錶,包括視圖,那我們就先把 所有select 用union all 串連後組成一個視圖,

然後使用這個視圖查詢就可以了。。。

修改如下

insert into a (id,name) 

select SEQ_A.nextval, t. * from (

<iterate conjunction="UNION ALL"  property="d">

select #d[].name# from dual

</iterate>

) t 

正常運行。。。

 

同時注意 
SELECT id,name FROM (select SEQ_B_LOG_ID.NEXTVAL id , ‘elong_deo‘ name from dual);

這樣也是不對的,違反第二條,在被查詢的視圖中使用序號

可以改成

select  SEQ_B_LOG_ID.NEXTVAL id,name from (select ‘elong_deo‘ name from dual);

 

3. --- The error occurred while applying a parameter map.
--- Check the doInsertFlightDynamic-InlineParameterMap.
--- Check the statement (update failed).
--- Cause: java.sql.SQLSyntaxErrorException: ORA-01745: 無效的主機/綁定變數名

sql語句中,兩個填充變數間沒有寫逗號

 

ibatis 批量插入oracle總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.