首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

insert …select 中用rand()解决办法

2012-01-18 
insert ……select 中用rand()我想在insert……select的时候使用rand()insertintob(comid,comname,xid)selectt

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

热点排行