您好,登錄后才能下訂單哦!
小編給大家分享一下如何從Oracle數據庫中讀取數據自動生成INSERT語句,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
Oracle INSERT 語句
方法1
我估計有點 SQL 基礎的人都會寫 INSERT 語句。下面是 SQL 標準寫法。
INSERT INTO employees (employee_id, name) VALUES (1, 'Zhangsan'); INSERT INTO employees VALUES (1, 'Shangbo');
方法2
其實, Oracle 還支持下面的寫法,作用和上面的語句完全相同。
INSERT INTO (SELECT employee_id, name FROM employees) VALUES (2, 'Lisi');
方法3
此外,同其他數據庫一樣,Oracle 也支持下面這種寫法。
INSERT INTO employees SELECT 3, 'Wangwu' FROM DUAL;
方法4
下面這種寫法可以實現列轉行,如我們有下面的表存儲原始數據,原始數據可能從文件中來。
create table sales_input_table ( prod_id number(9,0), amt_mon number(9,6), amt_tue number(9,6), amt_wed number(9,6), amt_thu number(9,6), amt_fri number(9,6) ); insert into sales_input_table values (1, 100.0, 200.0, 300.0, 400.0, 500.0);
下面我們通過一個 SQL 把上面的數據插入到下面的表中實現列轉行。
CREATE TABLE sales ( prod_id number(9,0), time_id date, amount number(9,0) ); INSERT ALL INTO sales (prod_id, time_id, amount) VALUES (prod_id, CURRENT_DATE, amt_mon) INTO sales (prod_id, time_id, amount) VALUES (prod_id, CURRENT_DATE + 1, amt_tue) INTO sales (prod_id, time_id, amount) VALUES (prod_id, CURRENT_DATE + 2, amt_wed) INTO sales (prod_id, time_id, amount) VALUES (prod_id, CURRENT_DATE + 3, amt_thu) INTO sales (prod_id, time_id, amount) VALUES (prod_id, CURRENT_DATE + 4, amt_fri) SELECT prod_id, amt_mon, amt_tue, amt_wed, amt_thu, amt_fri FROM sales_input_table;
方法5
下面這種寫法可以幫我們一次性把一個表中的數據倒入到多個表中,否則我們必須寫多條 SQL 實現同樣的功能。
INSERT ALL WHEN order_total <= 100000 THEN INTO small_orders WHEN order_total > 100000 AND order_total <= 200000 THEN INTO medium_orders WHEN order_total = 500000 THEN INTO special_orders WHEN order_total > 200000 THEN INTO large_orders ELSE INTO large_orders SELECT order_id, order_total, sales_rep_id, customer_id FROM orders;
注意,當 order_total 大于 200000 時,orders 會被插入到 large_orders 和 special_orders 中。這可能不是你想要的結果,如果你只想讓 orders 插入到 special_orders 表中,你只需要把 ALL 替換成 FIRST, 如下。
INSERT FIRST WHEN order_total <= 100000 THEN INTO small_orders WHEN order_total > 100000 AND order_total <= 200000 THEN INTO medium_orders WHEN order_total = 500000 THEN INTO special_orders WHEN order_total > 200000 THEN INTO large_orders ELSE INTO large_orders SELECT order_id, order_total, sales_rep_id, customer_id FROM orders;
從Oracle數據庫中讀取數據,自動生成INSERT語句
創建表
-- Create table create table TB_ACCIDENT_TYPE ( ID NUMBER(20) not null, NAME VARCHAR2(50), PATH VARCHAR2(20), PARENTPATH VARCHAR2(20), URL VARCHAR2(20), TYPE VARCHAR2(2), DESCR VARCHAR2(50) )
顯示表中的數據
select 'INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES(' || '''' || ID ||'''' || ',' || '''' || NAME || '''' || ',' || '''' || PATH || '''' || ',' || '''' || PARENTPATH || '''' || ',' || '''' || URL || '''' || ',' || '''' || TYPE || '''' || ',' || '''' || DESCR || '''' || ');' From tb_accident_type order by ID
顯示結果
INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('1','事故類型關聯 ','1','0','','0','');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('2','危險源類型關聯','2','0','','','');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('3','危險品類型關聯','3','0','','','');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('4','生產企業類型關聯','4','0','','','');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('5','區域關聯','5','0','','0','');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('568','物體打擊','1.1','1','','1','物體打擊');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('569','車輛傷害','1.2','1','','1','車輛傷害');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('570','機器傷害','1.3','1','','1','機器傷害');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('571','起重傷害','1.4','1','','1','起重傷害');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('572','觸電','1.5','1','','1','觸電');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('573','淹溺','1.6','1','','1','淹溺');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('574','灼燙','1.7','1','','1','灼燙');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('575','火災','1.8','1','','1','火災');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('576','高處墜落','1.9','1','','1','高處墜落');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('577','坍塌','1.10','1','','1','坍塌');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('578','冒頂片幫','1.11','1','','1','冒頂片幫');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('580','透水','1.12','1','','1','透水');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('581','波炮','1.13','1','','1','波炮');INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('582','火藥爆炸','1.14','1','','1','火藥爆炸')INSERT INTO tb_accident_type (ID,NAME,PATH,PARENTPATH,URL,TYPE,DESCR) VALUES('583','瓦斯爆炸','1.15','1','','1','瓦斯爆炸');
以上是“如何從Oracle數據庫中讀取數據自動生成INSERT語句”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。