declare @i int
set @i=1declare @value intwhile(@i<=(select count(*) from dbo.BA_LoopInfoDetails))
begin
select top 1 * from dbo.BA_LoopInfoDetails where lidid not in (select top @i lidid from dbo.BA_LoopInfoDetails)
set @i=@i+1
end提示: '@i' 附近有语法错误。这句该怎么写呢?

解决方案 »

  1.   

    sql2000不支持top @i 这种变量
      

  2.   

    declare @i int 
    set @i=1 declare @value int while(@i <=(select count(*) from dbo.BA_LoopInfoDetails)) 
    begin 
    select top 1 * from dbo.BA_LoopInfoDetails where lidid not in (select top (@i) lidid from dbo.BA_LoopInfoDetails) 
    set @i=@i+1 
    end 
      

  3.   

    存储过程实现吧,动态sql
    然后 exec @sql
      

  4.   

    while(@i <=(select count(*) from dbo.BA_LoopInfoDetails)) 
    begin 
    DECLARE @SQL VARCHAR(4000)
    SET @SQL='select top 1 * from dbo.BA_LoopInfoDetails where 
    lidid not in (select top '+LTRIM(@i)+' lidid from dbo.BA_LoopInfoDetails)')
    EXEC(@SQL)
    set @i=@i+1 
    end 
      

  5.   

    while(@i <=(select count(*) from dbo.BA_LoopInfoDetails)) 
    begin 
    DECLARE @SQL VARCHAR(4000)
    SET @SQL='select top 1 * from dbo.BA_LoopInfoDetails where 
    lidid not in (select top '+LTRIM(@i)+' lidid from dbo.BA_LoopInfoDetails)'
    EXEC(@SQL)
    set @i=@i+1 
    end 
      

  6.   

    对,不支持top @i 
    需要拼字符串