sql插入多条记录
㈠ 如何同时往数据库中插入多条记录
不同数据库之间略有不同,以几大主流数据库(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中的第一列(不管其列名)将用来填充表列中的指定的第一个列,第二列将用来填充表列中指定的第二个列,如此等等。