declare @i int
set @i=0
while @i <(select count(speak_id) from web..bbs_content)
begin
insert into FORUM_DOC(FR_NO,FR_ID,usr) SELECT TOP 10 A.SPEAK_ID,A.BBSMOD_ID,A.SPEAKER_ID From web..bbs_content AS A where A.speak_id not in
(SELECT TOP @i B.speak_id FROM web..bbs_content AS B ORDER BY B.speak_id)
set @i=@i+1
end
这个时候(SELECT TOP @i B.speak_id FROM web..bbs_content AS B ORDER BY B.speak_id)
里的变量@i找不到了,为什么呢?
------解决方案--------------------------------------------------------
什么问题没看清除,但用这个应该可以解决问题
原SQL语句改成:
exec ( 'insert into FORUM_DOC(FR_NO,FR_ID,usr) SELECT TOP 10 A.SPEAK_ID,A.BBSMOD_ID,A.SPEAKER_ID From web..bbs_content AS A where A.speak_id not in
(SELECT TOP '+@i+ ' B.speak_id FROM web..bbs_content AS B ORDER BY B.speak_id) ')