我有这个sp:使用sql server 2008
create procedure SelectTopCounts
@Id bigint = null,
@Count int = null,
@GetAll bit = 0
as
begin
set nocount on
IF (@Count IS NULL)
SELECT @Count = 15 --default
if(@GetAll = 1)
begin
select col,col2... .......
--very long select statement...
end
if(@Count is not null)
begin
select top @count .....
--very long select statement...
end
有没有办法我只能有一个select语句而不是在if and else
条件下重复?
答案 0 :(得分:1)
假设你的表总是有<= 20亿行,用这个替换所有的IF / ELSE:
SELECT TOP(COALESCE(CASE @GetAll WHEN 1 THEN 2000000000 END, @Count, 15))
col1, col2
FROM ...
-- I assume the @Id comes into play somehow...
-- WHERE ID_column = COALESCE(@Id, ID_column);