當前位置:首頁 » 編程語言 » sql插入多條記錄

sql插入多條記錄

發布時間: 2023-06-11 16:32:11

㈠ 如何同時往資料庫中插入多條記錄

不同資料庫之間略有不同,以幾大主流資料庫(oracle,sqlserver,mysql)為例。

oracle(以插入三條為例),結尾分號不可省略,否則會報錯。

insertintotestvalues(1,'張三');
insertintotestvalues(2,'李四');
insertintotestvalues(3,'王五');

㈡ 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 添加多條數據

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

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

㈣ 向sql 資料庫中增加多條數據,用sql命令怎麼寫急!急!急!

insert into 表名(列名1,列名2)
select 值1,值2
union all
select 值1,值2 union all
select 值1,值2

㈤ 如何用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;

(5)sql插入多條記錄擴展閱讀

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

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

熱點內容
歷史伺服器節點名怎麼查看 發布:2025-02-13 16:30:05 瀏覽:164
字典破解密碼失敗會顯示什麼 發布:2025-02-13 16:30:02 瀏覽:200
簡單的c語言編譯器 發布:2025-02-13 16:20:57 瀏覽:952
大眾空調登錄密碼多少 發布:2025-02-13 16:16:08 瀏覽:33
等差數列求和c語言 發布:2025-02-13 16:10:05 瀏覽:714
安卓手機如何無密碼解鎖vivo 發布:2025-02-13 15:52:10 瀏覽:885
電信50m上傳 發布:2025-02-13 15:47:56 瀏覽:364
清理outlook2010緩存 發布:2025-02-13 15:43:30 瀏覽:55
ohem演算法 發布:2025-02-13 15:40:10 瀏覽:931
java編程課程 發布:2025-02-13 15:36:08 瀏覽:476