使用Oracle插入文法來插入多條記錄____Oracle

來源:互聯網
上載者:User
問:怎樣才能在Oracle中只用一個INSERT來插入多條記錄?除了寫五條INSERT來插入五條記錄到一個表,我更傾向於用一條單獨的語句來執行插入。


答:根據PSOUG.org上的資訊,一條INSERT語句可以添加一條或多條記錄到一個關係型資料庫的任意一個表中。為了讓使用者插入記錄到一個表,此表必須是在使用者自己的模式中或者此使用者必須在此表上有插入對象的許可權。

如果使用的是Oracle 10g或以上版本,你就可以使用Insert All來插入來自多個表的多條資料記錄到一個表中:

INSERT ALL

INTO table (column1, column2, , ,)

VALUES (list of values)

SELECT ....

FROM table1, table2, ,

WHERE....;

如果插入來自於一個試算表或是逗點分隔檔的資料,你就可以要麼建立一個暫存資料表,要麼建立一個外部表格來載入資料。

與此同時,你還可以選擇以下方法:

INSERT INTO table_name (column_1, column_2, ..., column_n)

SELECT value_1, value_2, ..., value_n

FROM ...

另外,還可以在一個單獨步驟中使用以下Oracle插入文法來插入多條記錄:

INSERT ALL INTO mytable (column1, column2, column3) VALUES ('val1.1', 'val1.2', 'val1.3') INTO mytable (column1, column2, column3) VALUES ('val2.1', 'val2.2', 'val2.3') INTO mytable (column1, column2, column3) VALUES ('val3.1', 'val3.2', 'val3.3') SELECT * FROM dual;

還可以使用類似下面的命令來插入多個值到多個表中:

INSERT ALL INTO product (product_id,product_name) VALUES (1000, 'Disc') INTO product (product_id, product_name) VALUES (2000, 'Floppy') INTO customers (customer_id, customer_name, city) VALUES (999999, 'Anderson Construction', 'New York') SELECT * FROM dual;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.