Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 局部变量未显示所有值_Sql Server_Tsql - Fatal编程技术网

Sql server 局部变量未显示所有值

Sql server 局部变量未显示所有值,sql-server,tsql,Sql Server,Tsql,我是编程新手,所以我不知道该怎么问这个问题 我要做的是首先在不同的查询中查找@userid值,然后再查找 select a,b from tableA where userid = @userid and active=1 and payments=1 然后它执行以显示行数(假设:10行) 但当我喜欢下面的内容时,我只得到1行(我想得到全部10行): 所以,我请求帮助,我该怎么做。我必须像第2步那样做,因为我必须运行其他查询,@A和@B是,因为它们是标量变量,只能容纳1项,在您的情况下,它

我是编程新手,所以我不知道该怎么问这个问题

我要做的是首先在不同的查询中查找@userid值,然后再查找

select a,b
from tableA
where userid = @userid  
and active=1
and payments=1
然后它执行以显示行数(假设:10行)

但当我喜欢下面的内容时,我只得到1行(我想得到全部10行):


所以,我请求帮助,我该怎么做。我必须像第2步那样做,因为我必须运行其他查询,@A和@B

是,因为它们是标量变量,只能容纳1项,在您的情况下,它将容纳最后一行的值。您可能需要考虑使用表变量。像

DECLARE @tab1 table(
 A varchar(10),
 B bigint );
然后把它填满

insert into @tab1(A,B)
  select a, b
  from tableA
  where userid=@userid
  and active=1
  and payments=1
现在从中选择

select * from @tab1;

似乎您正试图对表a的所有a和b值进行处理

如果记录数较少,则可以使用游标,否则可以使用WHILE循环读取每条记录

Declare @a varchar(10)
Declare @b varchar(10)

DECLARE MyCursor CURSOR FOR  
select a, b
from tableA
where userid=@userid
and active=1
and payments=1

    OPEN MyCursor   
    FETCH NEXT FROM MyCursor INTO @a, @b   
WHILE @@FETCH_STATUS = 0   
BEGIN   
       Select @a, @b
       FETCH NEXT FROM MyCursor INTO @a, @b  
END   
CLOSE MyCursor   
DEALLOCATE MyCursor

我完全同意Rahul的说法,我将这个插入到#temp表中,所以当我插入到#temp(colm1,colm2)中时,从表a中选择a,b,其中userid=@userid,active=1,payment=1,然后我有所有行,好的,但我想使用a,b值来进行下面的其他查询。或者说:我必须计算行数,如果行数大于1且小于18,我必须更新临时表。在这种情况下,这是怎么可能的。
Declare @a varchar(10)
Declare @b varchar(10)

DECLARE MyCursor CURSOR FOR  
select a, b
from tableA
where userid=@userid
and active=1
and payments=1

    OPEN MyCursor   
    FETCH NEXT FROM MyCursor INTO @a, @b   
WHILE @@FETCH_STATUS = 0   
BEGIN   
       Select @a, @b
       FETCH NEXT FROM MyCursor INTO @a, @b  
END   
CLOSE MyCursor   
DEALLOCATE MyCursor