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

怎么把SELECT记录集中得某个值临时保存到一个变量

2012-02-15 
如何把SELECT记录集中得某个值临时保存到一个变量?declare@sTermIDvarchar(20)declare@nReturnintdeclare@

如何把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

热点排行
Bad Request.