插入一组数据

hling 2007-10-30 10:15:38
我要向表插入一组数据,如4,3,2,5,6,
插入变成
表:
id name wedid
1 a 4
2 a 3
3 a 2
4 a 5
5 a 6

怎么处理?除了用循环外,有没有办法一条插入语句就可以处理。
...全文
61 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
playwarcraft 2007-10-30
  • 打赏
  • 举报
回复

create table T(id int identity(1,1),name varchar(10) default 'a' ,wedid int)
Go

declare @s varchar(20)
set @s='4,3,2,5,6'

declare @sql varchar(500)
select @sql='insert T(wedid) select '+replace(@s,',',' union all select ')
exec(@sql)

select * from T
/*
id name wedid
----------- ---------- -----------
1 a 4
2 a 3
3 a 2
4 a 5
5 a 6

*/

drop table T

SatanAngel 2007-10-30
  • 打赏
  • 举报
回复
你插入的值也没有规律啊,如果是初始插入数据,就拷贝一下,改一下值
fa_ge 2007-10-30
  • 打赏
  • 举报
回复
借用临时表就好

create table #t
(id int identity(1,1),kk int )
insert into #t
4 union all
3 union all
2 union all
5 union all
6

update t
set wedid=b.kk
from t a left join #t b
on a.id=b.id
fa_ge 2007-10-30
  • 打赏
  • 举报
回复
借用临时表就好

create table #t
(id int identity(1,1),kk int )
insert into #t
4 union all
3 union all
2 union all
5 union all
6

update t
set wedid=b.kk
from t a left join #t b
on a.id=b.id

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧