SQL 最小行数

ref*_*cus 5 sql-server

我有一个允许用户传入参数的存储过程。

SELECT * 
FROM Table1
WHERE columnA =@paramA, ColumnB=@paramB....
Run Code Online (Sandbox Code Playgroud)

如果少于 10 行,我需要隐藏结果,但如果多于 10 行,则返回它们。有没有一种干净的方法来做到这一点?任何帮助都会很棒。

scs*_*mon 8

您可以使用count(*) over()来确保您的结果集有那么多行。

如果行数 = 10,您没有说明要做什么,只有 > 或 <,因此您可能需要根据需要将操作数更改为 >=。

数据库小提琴

select *
into mytable
from (select 'X' as c1) x
cross apply (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))as y(Y)

select * from mytable

--change the 10 to 11 to see it not return
select * from
(
select *, COUNT(*) OVER() CT
from mytable
where c1 = 'X'
) sub
where CT > 10
Run Code Online (Sandbox Code Playgroud)