0

一連のユニオンを含む行を返すストアド プロシージャがあります。私がやろうとしているのは、選択ステートメントが行を生成しないかどうかを確認し、そうであれば別のスクリプトを実行することです。

これは、ストアド プロシージャで使用している現在の SQL 選択です。どうすればこれを達成できるか教えてください。

SELECT  name AS [Name],  address1 AS Address1, address2 AS Address2, city AS City,  1 AS DeliveryTypeKey, email AS Email,  
  ( SELECT TOP 1   c_phone_number   
    FROM phone WITH (NOLOCK)  
    WHERE value = @value) AS Fax 
    FROM #Cities x    
 INNER JOIN prov p WITH (NOLOCK) ON x.ckey = p.pkey  
 UNION 
SELECT  name AS [Name], address1 AS Address1, address2 AS Address2, city AS City,  1 AS DeliveryTypeKey, email AS Email,  
  ( SELECT TOP 1 c_phone_number   
    FROM phone WITH (NOLOCK)  
    WHERE value = @value) AS Fax 
    FROM #Cities1 x    
    INNER JOIN prov1 p WITH (NOLOCK) ON x.ckey = p.pkey  
 UNION 
 SELECT  name AS [Name], address1 AS Address1, address2 AS Address2, city AS City,  1 AS DeliveryTypeKey, email AS Email,  
  ( SELECT TOP 1 c_phone_number   
    FROM phone WITH (NOLOCK)  
    WHERE value = @value) AS Fax 
 FROM #Cities2 x    
 INNER JOIN prov2 p WITH (NOLOCK) ON x.ckey = p.pkey 
4

1 に答える 1

1

ROWCOUNT 関数を使用して、返された行があるかどうかを確認します。

DECLARE @table TABLE 
  ( 
     DATA INT 
  ) 

INSERT @table 
VALUES (0) 

SELECT * 
FROM   @table 
WHERE  DATA > 0 

IF @@ROWCOUNT = 0 
  SELECT 'run' 
ELSE 
  SELECT 'Don''t run' 
于 2012-10-10T10:43:51.577 に答える