insert ……select 中用rand()
我想在insert ……select 的时候使用rand()
insert into b(comid,comname,xid) select top 10 id,comname,cast((20 * RAND()+1) as int) from a
效果是这样的
就是说从A表插B表的同时也插入一个随机值,
用我的语句只回是一个值,怎么样做才好呢,请大大们指点~
------解决方案--------------------insert into b(comid,comname,xid)
select top 10 id,comname,ABS(CHECKSUM(NEWID()))%21 from a
------解决方案--------------------是的,只有一个值
insert into b(comid,comname,xid) select top 10 id,comname,null from a
set rowcount 1
while exists (select 1 from b where xid is null)
update b
set xid=cast((20 * RAND()+1) as int)
where xid is null
set rowcount 0