How to check an empty table and end a stored procedure

If the table is not empty, then show the contents of the table and do not execute the rest of the script. How to achieve this? What is the best methodology? to install noexec or raiserror? or use Return?

Thank!

+3
source share
2 answers
if exists(select top 1 NULL from <your_table_name>)
begin
  --do something if you need

  select col1, col2,... from <your_table_name>
  where <your_condition>

  --do other things if needed
end
else
  return   <-- this will stop right here and return
+3
source

It depends on the use of the stored procedure in the context, but RETURNit is the simplest and easiest solution.

+4
source

All Articles