2013-04-13 89 views
2

在下面的SQL中,它在我的Excel表中找到「壞行」並將它們複製到另一個表中。 這工作完美。然而,在下面的每個OR分析中,我想讓列「rejectreason」=一些錯誤文本。 因此,例如,如果eventID爲= 0,則將行移動到表中,但也將列「rejectreason」更新爲文本「錯誤eventID等於0」。添加拒絕原因的SQL腳本

我還需要爲下面的所有其他OR語句做類似的操作。

我怎樣才能做到這一點

SQL

REPLACE INTO InvalidBaseDataTable SELECT * FROM BaseDataTable where dateTime = '0000-00-00 00:00:00'   

    OR eventId = 0       

    OR ueType = 0       

    OR eventId NOT IN (SELECT DISTINCT(eventId) FROM EventCauseTable) 

    OR causeCode < (SELECT MIN(causeCode) FROM EventCauseTable) 

    OR causeCode > (SELECT MAX(causeCode) FROM EventCauseTable) 

    OR ueType NOT IN (SELECT DISTINCT(tac) FROM UeTable) 

    OR 



     (eventId NOT IN (SELECT DISTINCT(eventId) FROM EventCauseTable) 

     AND 

     causeCode NOT IN (SELECT DISTINCT(causeCode) FROM EventCauseTable)) 

回答

3

你可以嘗試這樣的:

REPLACE INTO InvalidBaseDataTable 
SELECT *, CASE 
    WHEN dateTime = '0000-00-00 00:00:00' THEN 'datetime equal to 0' 
    WHEN eventId = 0 THEN 'eventId equal to 0' 
    WHEN ueType = 0 THEN 'ueType equal to 0' 
    ELSE 'All good' 
END AS rejectionreason 
FROM BaseDataTable WHERE dateTime = '0000-00-00 00:00:00'   
OR eventId = 0 
OR ueType = 0 
OR eventId NOT IN (SELECT DISTINCT(eventId) FROM EventCauseTable) 
OR causeCode < (SELECT MIN(causeCode) FROM EventCauseTable) 
OR causeCode > (SELECT MAX(causeCode) FROM EventCauseTable) 
OR ueType NOT IN (SELECT DISTINCT(tac) FROM UeTable) 
OR (eventId NOT IN (SELECT DISTINCT(eventId) FROM EventCauseTable) 
    AND 
    causeCode NOT IN (SELECT DISTINCT(causeCode) FROM EventCauseTable)) 
2

我會做這樣的事情。

特別是因爲你只想做一次測試,而不是一次找到問題,然後再次進行相同的測試只是爲了找到原因。

select * 
from (select id, 
       case when dateTime = '0000-00-00 00:00:00' then 'dateTime = 00000-00-00 00:00:00' else 
       case when eventId = 0 or ueType = 0 then 'eventId or ueType = 0' else 
       case when eventId not in (SELECT DISTINCT(eventId) FROM EventCauseTable) then 'eventId not in EventCauseTable' else 
       case when testMe2 not in (select value from valueList) then 'Value in testMe2 is not in Values' else null end end end end end result 
     from testThis) x 
where result is not null; 
相關問題