-1

表结构:

Customer
    ID int not null,
    Name varchar(100) null

询问:

SELECT ID, 
  (SELECT CASE WHEN (Name IS NULL AND Name <> '')
          THEN CAST(1 AS BIT)
          ELSE CAST(0 AS BIT) END
   FROM Customer) AS IsNameNullOrWhitespace

这给了我“子查询返回超过 1 个值这是 ..”。为什么是这样?

4

2 回答 2

2

看来您不需要子查询-您可以将 嵌入case到顶级 中select,如下所示:

SELECT id, 
       CASE 
         WHEN ( name IS NULL 
                AND name <> '' ) THEN Cast(1 AS BIT) 
         ELSE Cast(0 AS BIT) 
       END AS IsNameNullOrWhitespace 
FROM   customer 
于 2013-03-07T18:33:08.237 回答
1
SELECT ID, 
Case When IsNull(Name,'') ='' Then CAST(1 AS BIT) Else CAST(0 AS BIT) End  
IsNameNullOrWhitespace
FROM Customer

您需要以上查询。

于 2013-03-07T18:37:17.947 回答