如果没有找到行SQL,则返回一个值

时间:2010-04-21 02:10:13

标签: sql tsql

这是我的简单查询。如果我查询不存在的记录,那么我将不会返回任何内容。我更喜欢在那个场景中返回false(0)。寻找简单的方法来解释没有记录。

SELECT  CASE
            WHEN S.Id IS NOT NULL AND S.Status = 1 AND (S.WebUserId = @WebUserId OR S.AllowUploads = 1) THEN 1
            ELSE 0
        END AS [Value]

        FROM Sites S

        WHERE S.Id = @SiteId

15 个答案:

答案 0 :(得分:100)

这与Adam Robinson的相似,但使用ISNULL而不是COUNT。

SELECT ISNULL(
(SELECT 1 FROM Sites S
WHERE S.Id = @SiteId and S.Status = 1 AND 
      (S.WebUserId = @WebUserId OR S.AllowUploads = 1)), 0)

如果内部查询具有匹配的行,则返回1。外部查询(使用ISNULL)然后返回此值1.如果内部查询没有匹配的行,则它不返回任何内容。外部查询将此视为NULL,因此ISNULL最终返回0。

答案 1 :(得分:59)

SELECT CASE WHEN COUNT(1) > 0 THEN 1 ELSE 0 END AS [Value]

FROM Sites S

WHERE S.Id = @SiteId and S.Status = 1 AND 
      (S.WebUserId = @WebUserId OR S.AllowUploads = 1)

答案 2 :(得分:15)

这可能是一匹死马,当没有行存在时返回1行的另一种方法是UNION另一个查询并在表中不存在时显示结果。

SELECT S.Status, COUNT(s.id) AS StatusCount
FROM Sites S
WHERE S.Id = @SiteId
GROUP BY s.Status
UNION ALL --UNION BACK ON TABLE WITH NOT EXISTS
SELECT 'N/A' AS Status, 0 AS StatusCount
WHERE NOT EXISTS (SELECT 1
   FROM Sites S
   WHERE S.Id = @SiteId
) 

答案 3 :(得分:9)

类似的东西:

if exists (select top 1 * from Sites S where S.Id IS NOT NULL AND S.Status = 1 AND (S.WebUserId = @WebUserId OR S.AllowUploads = 1))
    select 1
else
    select 0

答案 4 :(得分:6)

我在这里阅读了所有的答案,花了一段时间来弄清楚发生了什么。以下是基于Moe Sisko和一些相关研究的答案

如果您的SQL查询未返回任何数据,则不存在具有空值的字段,因此ISNULL和COALESCE都不会按您的要求工作。通过使用子查询,顶级查询获取具有空值的字段,并且ISNULL和COALESCE将按您希望/期望的那样工作。

我的查询

select isnull(
 (select ASSIGNMENTM1.NAME
 from dbo.ASSIGNMENTM1
 where ASSIGNMENTM1.NAME = ?)
, 'Nothing Found') as 'ASSIGNMENTM1.NAME'

我的评论问题

select isnull(
--sub query either returns a value or returns nothing (no value)
 (select ASSIGNMENTM1.NAME
 from dbo.ASSIGNMENTM1
 where ASSIGNMENTM1.NAME = ?)
 --If there is a value it is displayed 
 --If no value, it is perceived as a field with a null value, 
 --so the isnull function can give the desired results
, 'Nothing Found') as 'ASSIGNMENTM1.NAME'

答案 5 :(得分:5)

没有匹配的记录表示没有记录返回。如果没有找到记录,那么0的“值”就没有了。您可以创建一个疯狂的UNION查询来执行您想要的操作,但更简单地检查结果集中的记录数量会更好,更好。

答案 6 :(得分:5)

您只需要用LEFT JOIN替换WHERE:

SELECT  CASE
        WHEN S.Id IS NOT NULL AND S.Status = 1 AND ...) THEN 1
        ELSE 0
    END AS [Value]

    FROM (SELECT @SiteId AS Id) R
    LEFT JOIN Sites S ON S.Id = R.Id

此解决方案还允许您返回每列的默认值,例如:

SELECT
    CASE WHEN S.Id IS NULL THEN 0 ELSE S.Col1 END AS Col1,
    S.Col2,
    ISNULL(S.Col3, 0) AS Col3
FROM
    (SELECT @Id AS Id) R
    LEFT JOIN Sites S ON S.Id = R.Id AND S.Status = 1 AND ...

答案 7 :(得分:1)

WITH TIES怎么样?

SELECT TOP 1 WITH TIES tbl1.* FROM 
        (SELECT CASE WHEN S.Id IS NOT NULL AND S.Status = 1 
                      AND (S.WebUserId = @WebUserId OR 
                           S.AllowUploads = 1)
                     THEN 1 
                     ELSE 0 AS [Value]
         FROM Sites S
         WHERE S.Id = @SiteId) as tbl1
ORDER BY tbl1.[Value]

答案 8 :(得分:0)

这可能是单向的。

SELECT TOP 1 [Column Name] FROM (SELECT [Column Name] FROM [table]
    WHERE [conditions]
    UNION ALL
    SELECT 0 )A ORDER BY [Column Name] DESC

答案 9 :(得分:0)

@ hai-phan使用LEFT JOIN的答案是关键,但是可能很难遵循。我有一个非常复杂的查询,也可能不返回任何内容。我只是将他的回答简化为以下需要的内容。应用于具有很多列的查询很容易。

;WITH CTE AS (
  -- SELECT S.Id, ...
  -- FROM Sites S WHERE Id = @SiteId
  -- EXCEPT SOME CONDITION.
  -- Whatever your query is
)
SELECT CTE.* -- If you want something else instead of NULL, use COALESCE.
FROM (SELECT @SiteId AS ID) R
LEFT JOIN CTE ON CTE.Id = R.ID

答案 10 :(得分:0)

DECLARE @col int; 
select @col = id  FROM site WHERE status = 1; 
select coalesce(@col,0);

答案 11 :(得分:0)

You should avoid using expensive methods. You dont need any column for TBL2. 

SELECT COUNT(*) FROM(
         SELECT TOP 1     1 AS CNT  FROM  TBL1 
         WHERE ColumnValue ='FooDoo'  ) AS TBL2

or

IF EXISTS (SELECT TOP 1 1 FROM TABLE1 AS T1 
                          WHERE T1.ColumnValue='VooDoo') 
   SELECT 1 
ELSE 
   SELECT 0

答案 12 :(得分:0)

您可以执行以下操作。

IF EXISTS ( SELECT * FROM TableName WHERE Column=colval)
BEGIN
   select 
select name ,Id from TableName WHERE Column=colval
END
ELSE
  SELECT 'test' as name,0 as Id

答案 13 :(得分:0)

我的孤单正在工作

可以通过将1 = 2更改为1 = 1的方式进行测试

select * from (
    select col_x,case when count(1) over (partition by 1) =1 then 1 else HIDE end as HIDE from (
    select 'test' col_x,1 as HIDE
    where 1=2
    union 
    select 'if no rows write here that you want' as col_x,0 as HIDE
    ) a
    ) b where HIDE=1

答案 14 :(得分:-1)

我喜欢 James Jenkins 对 ISNULL 检查的回复,但我认为他的意思是 IFNULL。 ISNULL 没有像他的语法那样的第二个参数,但 IFNULL 在检查表达式之后的第二个参数,如果发现 NULL 则替换。