2

在下面的 SQL 中,它在我的 excel 表中找到“坏行”并将它们复制到另一个表中。这很完美。但是,在下面的每个 OR 语句下,我想让列“rejectionreason”= 一些错误文本。因此,例如,如果 eventID = 0,则将该行移动到表中,但还将“rejectionreason”列更新为文本“错误 eventID 等于 0”。

我还需要对下面的所有其他 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))
4

2 回答 2

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))
于 2013-04-13T16:04:32.490 回答
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;
于 2013-04-13T16:12:15.430 回答