首页 新闻 搜索 专区 学院

怎样获得newid()的ID号

0
悬赏园豆:50 [已解决问题] 解决于 2008-07-20 08:28
<P>有这样的一个存储过程</P> <P>CREATE PROCEDURE SelectProblem <BR>@n int,<BR>@sectionID int<BR>AS<BR>declare @i int<BR>set @i=0<BR>while @i&lt;@n<BR>begin<BR>insert into PaperDetail (TitleID) values ('exec('select top '+cast(@n as varchar)+' [ID] from problem where sectionid='+cast(@sectionID as varchar)+' order by newid()')')<BR>set @i=@i+1<BR>end<BR>GO</P> <P>在inset(TitleID)  是取不到 那个随机产生的 [ID]  就是插不进去 ,这该怎么办拉</P> <P>恳请广大博友解答一下,小弟不胜感激!</P> <P>&nbsp;</P>
问题补充: 很感谢大家的解答 !!! 但是还没解决,期待中.......
liuyueguang的主页 liuyueguang | 初学一级 | 园豆:150
提问于:2008-07-17 12:27
< >
分享
最佳答案
0
感觉存储过程有问题,你可以把你的表和你要完成的操作做一下说明 就是想从problem表中随机取n个ID插入到PaperDetail 表中吧。 [code] WITH temp_A as ( select ID,random = newID() ,sectionID from problem where sectionID = @sectionID ), temp_B as( select ID,rn = ROW_NUMBER() OVER(ORDER BY random),sectionID FROM temp_A )INSERT INTO PaperDetail (TitleID) SELECT ID FROM temp_B WHERE temp_B.rn <= @n [/code]
玉开 | 大侠五级 |园豆:8822 | 2008-07-17 13:12
其他回答(3)
0
insert into PaperDetail (TitleID) values ('exec('select top '+cast(@n as varchar)+' [ID] from problem where sectionid='+cast(@sectionID as varchar)+' order by newid()')') 说实话这句本身有错误,太复杂了,你就直说是什么目的吧,也许有简单的实现方法。
风海迷沙 | 园豆:4453 (老鸟四级) | 2008-07-17 13:00
0
先查看'exec('select top '+cast(@n as varchar)+' [ID] from problem where sectionid='+cast(@sectionID as varchar)+' order by newid()')'这个句子是否执行成功,最好是declare @ID int set @ID = (exec('select top '+cast(@n as varchar)+' [ID] from problem where sectionid='+cast(@sectionID as varchar)+' order by newid()')),然后再执行插入语句
自强不息 | 园豆:165 (初学一级) | 2008-07-17 13:05
0
问题提得有水平.
中尉 | 园豆:252 (菜鸟二级) | 2008-07-17 18:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册