當前位置:首頁 » 編程語言 » sqlinsertinto多條

sqlinsertinto多條

發布時間: 2022-12-22 06:40:35

sql insert into select 語句 需要一次插多條

insert into select
語句功能是從一個表格中讀取數據,插入另一個表格。
所以,select 子句的結果集是多條記錄,那插入的就是多條。

例 :insert into table_b(a,b) select a,b from table_a;
如果table_a中有多條記錄,那都會一起插入 table_b中。

⑵ sql怎麼insert into 多條

insert into persons
(id_p, lastname , firstName, city )
values
(200,'haha' , 'deng' , 'shenzhen'),
(201,'haha2' , 'deng' , 'GD'),
(202,'haha3' , 'deng' , 'Beijing');

⑶ 求sql怎麼一次用insert 添加多條數據

如果是sqlserver支持多sql語句,你可以將所有的insert拼接成字元串,一起發送到伺服器

或者你可以批量插入另一個數據集的數據
insert xxx(id,name) select id,name from xxx

⑷ 求sql怎麼一次用insert 添加多條數據

用insert 添加多條數據:
insert into 表1(列名) select 列名 from 表2 --主鍵要自增
對於主鍵不自增的也可以處理,如:建一張臨時表,主鍵ID自增,然後取出要插入有的最大值,把最大值加到臨時表ID上再用:insert into 表1(ID,列名) select ID,列名 from 表2
insert into persons
(id_p, lastname , firstName, city )
values
(200,'haha' , 'deng' , 'shenzhen'),
(201,'haha2' , 'deng' , 'GD'),
(202,'haha3' , 'deng' , 'Beijing');

⑸ sql 一次插入多條記錄

在使用sql資料庫的時候,我們也許會需要一次像資料庫中添加多條記錄,那麼我們可以使用sql語句來實現,該語句具體如下:
--添加一條記錄
insert
into
tablename(col1,col2,col3)
values
(1,2,3)
--添加多條記錄
insert
into
tablename(col1,col2,col3)
select
3,4,5
union
all
select
6,7,8
--從另外的一張表中讀取多條數據添加到新表中
insert
into
tablename(col1,col2,col3)
select
a,b,c
from
tablea
--從其他的多張表中讀取數據添加到新表中
insert
into
tablename(col1,col2,col3)
select
a,b,c
from
tablea
where
a=1
union
all
select
a,b,c
from
tableb
where
a=2
上邊代碼中的into都可以省略!
上邊代碼中的union
all如果換成union,則相同記錄只插入一次,不會重復插入。
另外一種方法是sql
server2008特有的,所以,如果你不是sql
server2008,就不能使用這種方法了。
insert
into
mytable(id,name)values(7,'003'),(8,'004'),(9,'005')
create
table
[test]
(
[num_id]
int
primary
key
)
go
declare
@temp
int
set
@temp=1;
while
@temp<=1000000
begin
insert
into
[test]([num_id])
values(@temp)
set
@temp=@temp+1;
end
go
----------------------------------------------------------
--試試下面的方法
--2005
declare
@n
as
bigint;
set
@n
=
1000000;
with
base
as
(
select
1
as
n
union
all
select
n
+
1
from
base
where
n
<
ceiling(sqrt(@n))
),
expand
as
(
select
1
as
c
from
base
as
b1,
base
as
b2
),
nums
as
(
select
row_number()
over(order
by
c)
as
n
from
expand
)
select
n
from
nums
where
n
<=
@n
option(maxrecursion
0);
--2
create
function
dbo.fn_nums(@n
as
bigint)
returns
table
as
return
with
l0
as(select
1
as
c
union
all
select
1),
l1
as(select
1
as
c
from
l0
as
a,
l0
as
b),
l2
as(select
1
as
c
from
l1
as
a,
l1
as
b),
l3
as(select
1
as
c
from
l2
as
a,
l2
as
b),
l4
as(select
1
as
c
from
l3
as
a,
l3
as
b),
l5
as(select
1
as
c
from
l4
as
a,
l4
as
b),
nums
as(select
row_number()
over(order
by
c)
as
n
from
l5)
select
n
from
nums
where
n
<=
@n;
go
--2000
這個會比前兩個慢,但是前兩個2000不能用
create
table
dbo.nums(n
int
not
null
primary
key);
declare
@max
as
int,
@rc
as
int;
set
@max
=
1000000;
set
@rc
=
1;
insert
into
nums
values(1);
while
@rc
*
2
<=
@max
begin
insert
into
dbo.nums
select
n
+
@rc
from
dbo.nums;
set
@rc
=
@rc
*
2;
end
insert
into
dbo.nums
select
n
+
@rc
from
dbo.nums
where
n
+
@rc
<=
@max;
--------------------------------------------------------------------------------------------------------

⑹ 如何用SQL語句向一個表中插入多行記錄

insert一般是用來給表插入一條指定的列值的,但是,insert還存在另一種形式,可以利用它將一條select語句的結果插入表中。

這就是所謂的insert select,顧名思義,它是由一條insert語句和一條select語句組成的。假如你從另一張表中合並客戶列表到你的Custumers表,不需要每次讀取一行,然後再將它用insert插入,可以如下進行:

insert into Custumer(cust_id,

cust_cintact,

cust_name,

cust_email,

cust_address,

cust_country)

select cust_id,

cust_cintact,

cust_name,

cust_email,

cust_address,

cust_country

from CustNew;

(6)sqlinsertinto多條擴展閱讀

insert select中的列名為簡單起見,這個例子在insert和select語句中使用了相同的列名,但是,不一定要求列名匹配。事實上,DBMS甚至不關心select返回的列名,它使用的是列的位置。

因此,select中的第一列(不管其列名)將用來填充表列中的指定的第一個列,第二列將用來填充表列中指定的第二個列,如此等等。

⑺ 求sql怎麼一次用insert 添加多條數據

用遍歷可以呀 下面的owners就是你傳進去的List 遍歷每一條就是一個對象的數據

java"><insertid="insertOrUpdateOwners"parameterType="java.util.List">
INSERTINTOOWNER(
village_id,
building_id,
house_id,
owner_name,
dwell_time,
owner_phone,
id_number,
sex,
is_owner,
owners_reserve3
)
VALUES
<foreachcollection="owners"item="owner"index="index"separator=",">
(
#{owner.villageId},
#{owner.building_id},
#{owner.house_id},
#{owner.ownerName},
#{owner.dwellTime},
#{owner.ownerPhone},
#{owner.idNumber},
#{owner.sex},
#{owner.isOwner},
#{owner.ownersReserve3}
)
</foreach>
熱點內容
超凡先鋒配置不行怎麼辦 發布:2025-05-15 23:27:54 瀏覽:530
win7取消加密 發布:2025-05-15 23:26:37 瀏覽:470
不用internet打開ftp 發布:2025-05-15 23:06:00 瀏覽:153
sql字元串取數字 發布:2025-05-15 22:57:45 瀏覽:124
推薦編程課 發布:2025-05-15 22:34:12 瀏覽:618
表拒絕訪問 發布:2025-05-15 22:29:37 瀏覽:978
電腦怎樣解壓文件 發布:2025-05-15 22:25:32 瀏覽:439
dns伺服器怎麼看 發布:2025-05-15 22:17:27 瀏覽:151
3dm的壓縮包 發布:2025-05-15 22:09:23 瀏覽:662
和存儲字長 發布:2025-05-15 21:54:09 瀏覽:515