1. 程式人生 > >SQL中一次插入多條資料

SQL中一次插入多條資料

SQL中insert一次可以插入一條資料,我們有三種方法可以一次性插入多條資料。

1.

語法:select 欄位列表 into 新表 from 源表

注意事項:此種方法新表是系統自動建立,語句執行前不可以存在新表,並且新表只能保留源表的標識列特性,其他約束不能保留。

若只需要源表的資料結構,我們可以在語句中新增(top 0)

2.

語法:insert into 目的表 select 欄位列表 from 源表

注意事項:此種方法目的表必須在語句執行前存在,並且每一列要與源表對應。

在此處還有一些有趣的問題,當我使用以下程式碼來插入多條資料時:

select top 0 * into newstudent from
student insert into newstudent select * from student

這裡會發生這樣的報錯:

因為NewClass表中ClassId為標識列,所以我們不能插入值。

我們的解決辦法如下:

select top 0 * into newstudent from  student
set identity_insert newstudent on
insert into newstudent (classid,classname) select * from student

我們把newstudent 的標識列插入寫為顯示的,並且添加了列名欄位便可以插入了。

之後我們再建立一個新的NewClass2:

select top 0 *into NewClass2 from MyClass
set identity_insert NewClass2 on
insert into NewClass2(ClassId,ClassName) select* from MyClass

此時還會報錯,這是因為我們之前設定了newclass的標識列插入為on,我們必須先關閉後才可以往newclass2插入值,程式碼如下:

select top 0 *into NewClass2 from MyClass
set identity_insert newclass off
set identity_insert NewClass2 on insert into NewClass2(ClassId,ClassName) select* from MyClass

至此我們解決了使用第二種方法一次插入多條資料。

3.

語法:insert into 表(欄位列名) select 值 union select值

相關推薦

SQL插入資料

SQL中insert一次可以插入一條資料,我們有三種方法可以一次性插入多條資料。 1. 語法:select 欄位列表 into 新表 from 源表 注意事項:此種方法新表是系統自動建立,語句執行前不可以存在新表,並且新表只能保留源表的標識列特性,其他約束不能保留。 若只需要源表的資料結構,我們可以在語句中

oracle插入資料

insert into 表名(欄位1,欄位2) select '一','二' from dual         union all                                 select '三','四' from dual         union all              

oracle插入資料(insert all)

問題 公司的專案,有個功能每次使用需要向資料庫插入很多資料,導致頁面等待很長時間才有結果。 資料庫:oracle11g id:採用sequence自增 每次迴圈,都會查詢一次sequence,然後insert一條資料,效能非常低。 改進 改成一次

Mybatis使用註解的方式插入資料

ORACLE 對於oracle資料庫:不需要進行額外設定,可以直接在方法上使用註解進行多表的插入 @Insert("INSERT ALL INTO city (id,name,state) VALUES (31002, 'Disc','DISC') INTO city2

mybatis怎麼實現插入資料

前兩種為mybatis框架裡面的程式碼,第三種方法為純java程式碼時jdbc操作 1.複製原有資料庫中的n條記錄直接插入 <!--複製Menu表中的所有資訊複製插入,傳入引數為#{pr

sql insert into 一次性插入資料張表查詢到的資料插入到另張表

--插入多條資料使用DEFAULT關鍵字(第二種方法,不要將預設列名寫出,在UNION後面加上all,最後一行不加) ------------------------------------------------------------------------------

在Oracle執行sql語句,結束符很重要

有時我們需要一次性執行多條sql語句,而用來更新的sql是根據實際情況用程式碼拼出來的解決方案是把sql拼成下面這種形式:begin update TB_VG set seq = 1, vessel_id = 'Jin14', vessel_type = 'TRACK' w

如何在Oracle執行sql語句,結束符很重要

有時我們需要一次性執行多條sql語句,而用來更新的sql是根據實際情況用程式碼拼出來的解決方案是把sql拼成下面這種形式:begin  update TB_VG set seq = 1, vessel_id = 'Jin14', vessel_type = 'TRACK' where batch_number

Sql server儲存過程以及插入記錄

首先簡單介紹sql server儲存過程吧。至於概念含義啥的就不做過多介紹了。它其實和mysql有些類似,語法大同小異。 還是用例子說明問題吧。CREATE PROCEDURE insert_supplier @supplier_name varchar(30), @sup

mysql插入demo測試用資料

1.傻傻的insertINSERT INTO raw_materials.common_picture_setting (PICTURE_SETTING_NAME,PICTURE_SETTING_URL

oracle Insert 插入記錄

pan rac ora 方法 tab where ble code 兩種方法 oracle Insert 一次插入多條記錄有兩種方法: 1)Insert All Into table_name values ... insert all into table_name v

使用JDBC插入記錄(以MySQL為例)

閱讀本文需要的先修知識: 最基本的SQL語句 最基本的JDBC操作(如插入單條記錄) 如急需使用請直接看最後一段程式碼。 在JDBC中,插入記錄最簡單的方法是使用executeUpdate()方法,但該方法中的引數只能是單條SQL語句,其實對於需要INSERT或者UPDA

實現更新資料

實現用一條sql去更新多條語句的前提是多條語句的條件不同,但是更新的欄位及欄位的值都是相同的,這樣才能去更新。 public void updateMoreNumber(String[] ids){ StringBuffer sb = new StringBuffer("up

Mongoose - 錄入資料insertMany

使用 insertMany // 出入資料 { "list":[ { "areaOfPharmacistBackup":"湖北省武漢市黃陂區", "nameOfPharmacistBackup":"魯**", "typeOfPharmacistBackup":"中

插入

sel sysdate 插入 bcd xid sys user lec creat insert into erp_ckscd ( ckdbh, cklx, kfxxid,

Oracle 怎樣插入記錄

Oracle 一次性插入多條記錄跟 MYSQL 有很大不同。MYSQL  是這樣的,但在 Oracle 中行不同。 INSERT INTO Persons (LastName, Address) VALUES ('Wilson', 'Champs-Elysees'),('Gates', 'Champs-E

OracleInsert資料

insert all into JK_TB_DATE (fbmmc,fgzjh,fsbmc,fsbxh,fsbbh,db_shuifenyi,db_pihao,db_wuliaobianma) values ('檢測督查科','102','水分測試儀','SDWE-BZDHX

mysql使用遊標 觸發 插入記錄

DROP TRIGGER IF EXISTS `AutoInsert`; CREATE DEFINER=`root`@`localhost` TRIGGER `AutoInsert` AFTER INSERT ON `ml_exam` FOR EACH ROW begin

在oracle執行語句

工作中需要一次執行多條語句,本來想直接使用sql拼接成一個字串進行批處理,原sql如下: String sql = ""; for(int i=0; i<deviceInfo.getDevice_ip().length; i++){

sql語句優化進行記錄的-----插入和修改

更新: update t_student set name = 'timy' where id = 10 現在我要更新ID為10、12 、13的age等於10、12、13 UPDATE t_student SET age= CASEWHEN id 10 THEN10WHE