私はこれをやろうとしました:
if OBJECT_ID('a_b_Stage2_supA_demB') is not null
and (select COUNT(*) from a_b_Stage2_supA_demB) > 0
ただし、「and」条件の最初の側がすでに失敗しているにもかかわらず、SQL Server はa_b_Stage2_supA_demB
2 番目の条件をチェックしようとするため、存在しない場合はエラーを返します。
手伝ってくれてありがとう!
私はこれをやろうとしました:
if OBJECT_ID('a_b_Stage2_supA_demB') is not null
and (select COUNT(*) from a_b_Stage2_supA_demB) > 0
ただし、「and」条件の最初の側がすでに失敗しているにもかかわらず、SQL Server はa_b_Stage2_supA_demB
2 番目の条件をチェックしようとするため、存在しない場合はエラーを返します。
手伝ってくれてありがとう!
DMV は問題を簡単に解決できます。
IF EXISTS (select * from sys.tables t
INNER JOIN sys.partitions p on t.object_id = p.object_id
WHERE t.name = 'a_b_Stage2_supA_demB' and p.rows > 0)
.....
残念ながら、それは不可能です。SQL Server は、コンパイル前にすべてのオブジェクト参照 (ベース コード パス内) が存在することを確認します。if ステートメントがないのはどうですか?
begin try
exec('declare @x int = (
select 1 / COUNT(*)
from a_b_Stage2_supA_demB)');
print 'true';
end try
begin catch
print 'false';
end catch
書くだけ:
IF OBJECT_ID('a_b_Stage2_supA_demB') is not null
IF select COUNT(*) from a_b_Stage2_supA_demB > 0
.....
IF OBJECT_ID('dept') is not NULL
BEGIN
IF exists(SELECT 1 FROM dept)
PRINT 'exists with rows'
END