2012-03-09 104 views
1

我試圖插入多個行到一個SQL,Oracle表雖然SQL Developer版本3.0.040行插入 - 我該如何解決這個問題?

數據庫是由統一設置,所以我不知道它是什麼版本。

查看在線我已經拿出下面的代碼,但它不會插入任何數據。我已經測試了插入只有一行,這是確定的。我錯過了什麼?

Insert all 
    Into Patient Values 
    ('101', '1 house', Null, 'Kingston', 'Surrey', 'KT1 1XX', '10/jan/1980', 'm', 01452987456) 
    Into Patient Values 
    ('102', '2 egg rd', 'vail', 'guildford', 'Surrey', 'GU1 1LL', '05/dec/1985', 'm', 01452987456) 
    Into Patient Values 
    ('103', '6 station rd', Null, 'guildford', 'Surrey', 'GU1 2XX', '15/may/1990', 'f', 01452987456) 

Select * from Patient; 
+0

我從來沒有看到'插入所有'...你需要它是一個單一的指令插入? – 2012-03-09 14:10:03

+0

要獲得版本'從雙選擇v $版本;' – 2012-03-09 15:27:19

+0

如果您收到任何錯誤,會發生什麼錯誤? – 2012-03-09 15:28:03

回答

6

INSERT ALL有兩種不同的用途。一種是將不同的選定列的子集插入表中。另一種是根據特定的標準將行引入不同的行。在這兩種情況下,數據都來自SELECT子句,而不是來自VALUES。 See the examples in the documentation

通常情況下,您只需將可能寫入多個INSERT語句在一個單一的PL/SQL塊。像

begin 
    Insert Into Patient Values 
    ('101', '1 house', Null, 'Kingston', 'Surrey', 'KT1 1XX', '10/jan/1980', 'm', 01452987456); 
    Insert Into Patient Values 
    ('102', '2 egg rd', 'vail', 'guildford', 'Surrey', 'GU1 1LL', '05/dec/1985', 'm', 01452987456); 
    Insert Into Patient Values 
    ('103', '6 station rd', Null, 'guildford', 'Surrey', 'GU1 2XX', '15/may/1990', 'f', 01452987456); 
end; 
/

的東西,如果你真的想這樣做,在一個SQL語句,你可以做一個INSERT ... SELECT但是這通常會比使用三個獨立的報表更加複雜。

insert into patient 
    select * 
    from (select '101' id, '1 house' addr, null col1, 'Kingston' city, ... 
      from dual 
      union all 
      select '102', '2 egg rd', 'vail', 'guildford', 'Surrey', 'GU1 1LL', '05/dec/1985', 'm', 01452987456 
      from dual 
      union all 
      select '103', '6 station rd', Null, 'guildford', 'Surrey', 'GU1 2XX', '15/may/1990', 'f', 01452987456 
      from dual) 

我還要提醒你使用正確的數據類型,並在您INSERT語句中指定的列名。我猜測,例如,Patient表的第一列是某種PatientID,它被定義爲NUMBER。如果是這樣,你真的想要插入一個數字而不是一個字符串。類似地,第15列的'15/may/1990'值可能在表中定義爲DATE。如果是這樣,你INSERT應該明確地調用TO_DATE與特定的格式掩碼或使用ANSI日期格式,即date '1980-01-10'插入DATE不是一個字符串。如果您希望最後一列保留前導0,則需要確保數據庫中的列被定義爲VARCHAR2,並且您插入的是字符串而不是數字。

+1

@APC - 我希望我可以+1編輯! – 2012-03-09 17:54:59

+0

不客氣! – APC 2012-03-09 21:42:27

+0

謝謝Justin!你們所有的假設都是對的,儘管我並不想在開始時引用這個ID。 這兩種解決方案都運行良好,但我喜歡第二種解決方案的響應,因爲它告訴您插入的行數與第一種解決方案相反,即剛剛說「匿名塊已完成」。我猜如果我說明可能會改變的列名稱。 – MrPickles 2012-03-12 11:15:02

1

有趣。在嘗試時可以使用insert all來插入多行。並不是說我會推薦Justin提出的解決方案。

syntax diagram多表插頁指示子查詢總是需要。但是,您不必使用任何的子查詢的結果:

SQL> drop table t; 

Table dropped. 

SQL> create table t (c1 number, c2 varchar2(10)); 

Table created. 

SQL> insert all into t (c1, c2) values (1, 'one') 
    2  into t (c1, c2) values (2, 'two') 
    3 select * from dual; 

2 rows created. 

SQL> select * from t; 

     C1 C2 
---------- ---------- 
     1 one 
     2 two 

兩個into ... values(...)會導致兩行每行被插入子查詢:

SQL> insert all into t (c1, c2) values (1, 'one') 
    2  into t (c1, c2) values (2, 'two') 
    3 select * from dual 
    4 connect by level <= 10; 

20 rows created. 
+0

感謝您的幫助。不幸的是,第一篇文章更多的是我正在尋找的東西。 – MrPickles 2012-03-12 11:16:17

相關問題