首页 > 解决方案 > SQL Server - 选择语句包括变量

问题描述

这是使用变量并最终显示 Vol1 >0 和 vol1 <=10 之间的记录数的正确方法吗?

declare @x int;
set @x=0;
select
    case when vol1 > 0 and vol1 <=10 then x = x + 1 end
from coverage
group by vol1
print @x

标签: sqlsql-server

解决方案


如果我理解正确,您需要在 中分配变量select并摆脱group by,因此查询仅返回一行:

declare @x int;
set @x = 0;

select @x = count(*)
from coverage
where vol1 > 0 and vol1 <= 10;

print @x;

推荐阅读