日期:2014-05-19  浏览次数:20571 次

如何把SELECT记录集中得某个值临时保存到一个变量?
declare   @sTermID   varchar(20)
declare   @nReturn   int
declare   @nCount     int
set           @nCount   =   1

while   @nCount   <   41
      BEGIN
            Set   @sTermID   =   Select   top   1   termid   from   aaa   where   id=@nCount
            Set   @nReturn   =   Select   count(termid)   from   bbb   where   termid=   @sTermID
           
            UPDATE   MyTempTable   SET   use_times   =   @nReturn   where   id=@nCount
            set   @nCount   =   @nCount   +   1
      END

这段代码,两个Select句子都有问题:请问怎么修正??



------解决方案--------------------
declare @sTermID varchar(20)
declare @nReturn int
declare @nCount int
set @nCount = 1

while @nCount < 41
BEGIN
Select top 1 @sTermID =termid from aaa where id=@nCount
Select @nReturn = count(termid) from bbb where termid= @sTermID

UPDATE MyTempTable SET use_times = @nReturn where id=@nCount

set @nCount = @nCount + 1
END
------解决方案--------------------
declare @sTermID varchar(20)
declare @nReturn int
declare @nCount int
set @nCount = 1

while @nCount < 41
BEGIN
Set Select top 1 @sTermID = termid from aaa where id=@nCount
Set Select @nReturn = count(termid) from bbb where termid= @sTermID

UPDATE MyTempTable SET use_times = @nReturn where id=@nCount
set @nCount = @nCount + 1
END
------解决方案--------------------

declare @sTermID varchar(20)
declare @nReturn int
declare @nCount int
set @nCount = 1

while @nCount < 41
BEGIN
Select top 1 @sTermID=termid from aaa where id=@nCount
Select @nReturn=count(termid) from bbb where termid= @sTermID

UPDATE MyTempTable SET use_times = @nReturn where id=@nCount
set @nCount = @nCount + 1
END

------解决方案--------------------
declare @sTermID varchar(20)
declare @nReturn int
declare @nCount int
set @nCount = 1

while @nCount < 41
BEGIN
Set @sTermID =(Select top 1 termid from aaa where id=@nCount)
Set @nReturn =(Select count(termid) from bbb where termid= @sTermID)

UPDATE MyTempTable SET use_times = @nReturn where id=@nCount
set @nCount = @nCount + 1
END