如何一次插入多条记录的SQL语句?

如何一次插入多条记录的SQL语句?,第1张

在使用SQL数据库的时候,我们也许会需要一次像数据库中添加多条记录,那么我们可以使用SQL语句来实现,该语句具体如下:\x0d\x0a--添加一条记录\x0d\x0aINSERT INTO tableName(col1,col2,col3) VALUES (1,2,3)\x0d\x0a \x0d\x0a--添加多条记录\x0d\x0aINSERT INTO tableName(col1,col2,col3)\x0d\x0aSELECT 3,4,5\x0d\x0aUNION ALL \x0d\x0aSELECT 6,7,8\x0d\x0a \x0d\x0a--从另外的一张表中读取多条数据添加到新表中\x0d\x0aINSERT INTO tableName(col1,col2,col3) \x0d\x0aSELECT a,b,c FROM tableA\x0d\x0a \x0d\x0a--从其他的多张表中读取数据添加到新表中\x0d\x0aINSERT INTO tableName(col1,col2,col3)\x0d\x0aSELECT a,b,c FROM tableA WHERE a=1\x0d\x0aUNION ALL \x0d\x0aSELECT a,b,c FROM tableB WHERE a=2 \x0d\x0a上边代码中的into都可以省略!\x0d\x0a上边代码中的union all如果换成union,则相同记录只插入一次,不会重复插入。\x0d\x0a另外一种方法是SQL Server2008特有的,所以,如果你不是SQL Server2008,就不能使用这种方法了。 \x0d\x0aINSERT INTO MyTable(ID,NAME)VALUES(7,'003'),(8,'004'),(9,'005')\x0d\x0acreate table [TEST]\x0d\x0a(\x0d\x0a [NUM_ID] int primary key\x0d\x0a)\x0d\x0ago\x0d\x0adeclare @temp int\x0d\x0aset @temp=1\x0d\x0awhile @temp 回答于 2022-12-11

在使用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

--------------------------------------------------------------------------------------------------------


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11400268.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-15
下一篇2023-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存