如何检查空表并终止存储过程

use*_*076 3 sql sql-server-2008

如果表不为空,则显示表的内容并且不执行脚本的其余部分。如何实现这一目标?最好的方法是什么?设置 noexec 或 raiserror?或使用返回?

谢谢!

khe*_*eya 5

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
Run Code Online (Sandbox Code Playgroud)