當前位置:首頁 » 編程語言 » 一條sql插入多行數據
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

一條sql插入多行數據

發布時間: 2022-08-05 01:52:12

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

(1)一條sql插入多行數據擴展閱讀

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

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

Ⅱ 如何在sql中在新建表中插入多行數據

直接通過insert語句多次插入即可。
假如表名是
tablename
insert
into
tablename
values('value1','value2','value3',....);
insert
into
tablename
values('value11','value22','value33',....);
insert
into
tablename
values('value111','value222','value333',....);
備註:上面的參數個數根據實際需要調整即可。

Ⅲ sql 插入多行數據

-- or
insert into library

select '445501','TP3/12','資料庫導論','王強','科學出版社',17.90
union select '445502','TP3/12','資料庫導論','王強','科學出版社',17.90
union select '445503','TP3/12','資料庫導論','王強','科學出版社',17.90

Ⅳ SQL 如何同時插入多條記錄

第一個方法:
INSERT INTO MyTable(ID,NAME) VALUES(1,'123');
INSERT INTO MyTable(ID,NAME) VALUES(2,'456');
INSERT INTO MyTable(ID,NAME) VALUES(3,'789');

第二種方法,使用UNION ALL來進行插入操作:
INSERT INTO MyTable(ID,NAME)
SELECT 4,'000'
UNION ALL
SELECT 5,'001'
UNION ALL
SELECT 6,'002'

是不是要比第一種方法簡單點,據說要比第一種要快!

Ⅳ 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;
--------------------------------------------------------------------------------------------------------

Ⅵ Oracle資料庫,一條SQL語句插入多行數據

按照你現有的表創建歷史數據:

createtablekhqfbd_1
(khnint,
khqxhint,
khmcvarchar2(20),
qsrqvarchar2(8),
jsrqvarchar2(8),
bzvarchar2(100));

insertintokhqfbd_1values(2024,1,'第一季度','20240101','20240331','1');
insertintokhqfbd_1values(2024,2,'第二季度','20240401','20240630','1');
insertintokhqfbd_1values(2024,3,'第三季度','20240701','20240930','1');
insertintokhqfbd_1values(2024,4,'第四季度','20241001','20241231','1');
commit;

執行:

declare
v_yearint;
begin
selectmax(khn)intov_yearfromkhqfbd_1;
insertintokhqfbd_1
values
(v_year+1,1,'第一季度',v_year+1||'0101',v_year+1||'0331','1');
insertintokhqfbd_1
values
(v_year+1,2,'第二季度',v_year+1||'0401',v_year+1||'0630','1');
insertintokhqfbd_1
values
(v_year+1,3,'第三季度',v_year+1||'0701',v_year+1||'0930','1');
insertintokhqfbd_1
values
(v_year+1,4,'第四季度',v_year+1||'1001',v_year+1||'1231','1');
commit;
end;

執行後結果:

Ⅶ SQL在一個列中插入多行數據

無任何邏輯的數字,這個要看是什麼資料庫,各個資料庫產生隨機數的方法是不一樣的。
另外
「我要在wo那一列中添加三行無任何邏輯的數字 」
這個是用update不是insert
看你的應該是sql server資料庫:sql如下
update Brother set wo=dbo.udf_GetRandomInteger(1,100)

Ⅷ 如何實現一條sql語句插入多行數據

2種方案
1)
insert into tab1(fld1, fld2....fldn)
SELECT field1, field2....fieldn fom tab2 where xxxxx

2) insert into tab1(fld1, fld2....fldn) VALUES(1, 11, ....1111), (2, 22, ..., 2222), .....,(n, nn,...., nnnn)

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

可以一次加入多條記錄。
在SQL
SERVER里邊,多個INSERT
語句之間,用分號(;)或者空格,隔開,這樣資料庫就認為你是在進行多條SQL語句的插入操作。就可以插入多條了。

Ⅹ 關於SQL一次插入多行數據

1.打開Sql Server Enterprise Manager
2.打開database,選擇要導入數據的資料庫
3.右鍵All tasks-->import data
4.在data source中選擇Microsoft excel 97-2000
5.再選擇某一excel文件,並且這個文件不能被打開
就可以順利的導入了