[英]Sql select union with count
I have a stored procedure which returns rows with a bunch of unions. 我有一个存储过程,它返回带有一堆联合的行。 What I am trying to do is see if the select statement does not yield any rows, and if so, execute another script.
我要尝试执行的操作是查看select语句是否不产生任何行,如果是,则执行另一个脚本。
This is the current sql select I am using in the stored procedure. 这是我在存储过程中使用的当前SQL选择。 Please let me know how I can achieve this.
请让我知道我如何实现这一目标。
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
Use the ROWCOUNT function to check if any rows where returned: 使用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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.