Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

retornar apenas os últimos resultados selecionados do procedimento armazenado


Use uma variável de tabela:
create procedure xxxSearch @a nvarchar(80), @b nvarchar(80)...
as 
begin
  DECLARE @res TABLE(...)
  INSERT INTO @res(...)
  select whatever 
    from MyTable t
    where ((@a is null and t.a is null) or (@a = t.a)) and
          ((@b is null and t.b is null) or (@b = t.b))...

    if @@ROWCOUNT = 0
    begin
        INSERT INTO @res(...)
        select whatever 
          from MyTable t
          where ((@a is null) or (@a = t.a)) and
                ((@b is null) or (@b = t.b))...
          if @@ROWCOUNT = 0
          begin
             ...
          end
    end
    SELECT ... FROM @res
end