2011-03-17 184 views
3

我正在寫一個查詢,確實對數據庫表各種檢查,並返回結果的摘要(在一個單一的結果集)UNION與IF語句

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 

結果表應該是這樣的:

name     result description 
password check   COMPLETED OK 
contex properties check ERROR  no context property has been entered 

我知道語法是不正確的,但我想不出有什麼辦法來實現這一點。

回答

1
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 
+0

我認爲這是最好的。似乎非常可擴展和乾淨:) – Joe 2011-03-17 18:47:56

1

也許通過這樣做:

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) 
+0

該解決方案看起來最乾淨的,但我只是有點擔心,這可能會導致性能開銷在爲每次檢查執行相同的查詢兩次。 – Joe 2011-03-17 18:41:46

2

你可以試試這個:

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