Can you not raise errors within a case statement in T-SQL? I always have problems with SQL case statements :/
begin try
declare @i int
--set @i = (select COUNT(1) from table_name)
select Item_Num =
CASE (select COUNT(1) from table_name)
when 1 then (select Item_Num from table_name)
when 0 then (raiserror('No records in database', 0, 0))
ELSE (raiserror('Multiple records in database', 0, 0))
END
from table_name
end try
begin catch
declare @errormsg nvarchar(1024),
@severity int,
@errorstate int;
select @errormsg = error_message(),
@severity = error_severity(),
@errorstate = error_state();
raiserror(@errormsg, @severity, @errorstate);
end catch
Think of Case/When as operating on a single piece of data. If you think of it this way, a lot of your problems will go away.
If/Then is used to control the flow of logic.
Something like this should work for you.
declare @i int
set @i = (select COUNT(1) from table_name)
If @i = 1
Begin
Print "1 row"
End
Else If @i = 0
Begin
Print "no rows"
End
Else
Begin
Print "too many rows"
End