首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UNION with IF语句

UNION with IF语句
EN

Stack Overflow用户
提问于 2011-03-18 01:46:40
回答 3查看 14.6K关注 0票数 3

我正在编写一个查询,它对数据库表执行各种检查,并返回结果的摘要(在单个结果集中)

代码语言:javascript
复制
IF ( select COUNT(*) from member_table WHERE password IS NULL ) > 0
   SELECT 'password check' as name, 'ERROR' as result ,'there is user(s) with blank password' as description
ELSE
   SELECT 'password check' as name, 'COMPLETED' as result, 'OK' as description

UNION

IF ( select COUNT(*) from server_context_properties ) = 0
   SELECT 'context properties check' as name, 'ERROR' as result ,'no context property has been entered' as description
ELSE
   SELECT 'context properties check' as name, 'COMPLETED' as result, 'OK' as description

结果表应如下所示:

代码语言:javascript
复制
name                    result  description
password check          COMPLETED   OK
contex properties check ERROR       no context property has been entered

我知道语法是不正确的,但我想不出任何方法来实现这一点。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-18 02:09:20

代码语言:javascript
复制
Select Properties.Name, Properties.Result, Properties.Description
From    (
        Select 'password check', 1 As value, 'ERROR' As Result, 'There is a user with a blank password' As Description
        Union All Select 'password check', 0, 'COMPLETED', 'OK'
        Union All Select 'context properties check', 0, 'ERROR', 'No context property has been entered'
        Union All Select 'context properties check', 1, 'COMPLETED', 'OK'
        ) As Properties
    Join    (
            Select 'password check' As name
                , Case
                    When Exists( Select 1 From member_table Where password Is Null ) Then 1
                    Else 0
                    End As Value
            Union All 
            Select 'context properties check'
                , Case
                    When Exists( Select 1 From server_context_properties ) Then 1
                    Else 0
                    End
            ) As Results
        On Results.Name = Properties.Name
            And Results.Value = Properties.Value
票数 1
EN

Stack Overflow用户

发布于 2011-03-18 01:53:31

您可以尝试这样做:

代码语言:javascript
复制
DECLARE @passwordcheck INT, @contextcheck INT

SELECT @passwordcheck = COUNT(*) 
FROM member_table 
WHERE [password] IS NULL

SELECT @contextcheck = COUNT(*) 
FROM server_context_properties

SELECT  'password check' as name, 
        CASE WHEN @passwordcheck > 0 THEN 'ERROR' ELSE 'COMPLETED' END as result,
        CASE WHEN @passwordcheck > 0 THEN 'there is user(s) with blank password' ELSE 'OK' as description
UNION
SELECT  'context properties check' as name, 
        CASE WHEN @contextcheck = 0 THEN 'ERROR' ELSE 'COMPLETED' END as result,
        CASE WHEN @contextcheck = 0 THEN 'no context property has been entered' ELSE 'OK' END as description
票数 2
EN

Stack Overflow用户

发布于 2011-03-18 01:50:22

也许可以这样做:

代码语言:javascript
复制
SELECT 'password check' as name, 'ERROR' as result ,'there is user(s) with blank password' as description
WHERE EXISTS ( select * from member_table WHERE password IS NULL )
UNION
SELECT 'password check' as name, 'ERROR' as result ,'there is user(s) with blank password' as description
WHERE NOT EXISTS ( select * from member_table WHERE password IS NULL )

UNION

SELECT 'context properties check' as name, 'ERROR' as result ,'no context property has been entered' as description
WHERE NOT EXISTS (select * from server_context_properties)
UNION
SELECT 'context properties check' as name, 'COMPLETED' as result, 'OK' as description
WHERE EXISTS (select * from server_context_properties)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5343057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档