CASE声明

时间:2011-01-20 14:49:55

标签: sql sql-server

只是一个奇怪的问题。我使用CASE语句编写了以下代码,但我没有数据,所以我不确定我的逻辑是否正确。我正在做的是我在每个字段上应用长度检查,如果它不匹配,那么把它放在错误表中。那么,任何人都能告诉我我的逻辑是否正确吗?

查询:

 INSERT INTO
                [Elig].[dbo].[ErrorTable]
                (
                 [SeqId],
                 [CodeId],
                 [SubjectArea],
                 [FieldName],
                 [TableName],
                 [ErrorValue],
                 [ActiveFlag]
                )
                SELECT
                    [sd].[SuscriberDataId] AS [SeqId],
                    @InvalidLength AS [CodeId],
                    @SubjectArea AS [SubjectArea],
            CASE WHEN len(ltrim(Rtrim([sd].[SubscriberNumber]))) > 9 THEN 'SubscriberNumber'
                 WHEN len(ltrim(Rtrim([sd].[SubscriberLastName]))) > 35 THEN 'SubscriberLastName'
                 WHEN len(ltrim(Rtrim([sd].[SubscriberFirstName]))) > 15 THEN 'SubscriberFirstName'
                 WHEN len(ltrim(Rtrim([sd].[SubscriberMiddleInitial]))) > 1 THEN 'SubscriberMiddleInitial'  
                 WHEN len(ltrim(Rtrim([sd].[Sex]))) > 1 THEN 'Sex'
                 WHEN len(ltrim(Rtrim([sd].[Dob]))) > 8 THEN 'Dob'   
                 WHEN len(ltrim(Rtrim([sd].[Ssn]))) > 9 THEN 'Ssn'
                 WHEN len(ltrim(Rtrim([sd].[FacetsGroup]))) > 8 THEN 'FacetsGroup'  
           END AS FieldName,
           'Eligibility.SubscriberData',
           CASE  WHEN len(ltrim(Rtrim([sd].[SubscriberNumber]))) > 9 THEN [SubscriberNumber]
                 WHEN len(ltrim(Rtrim([sd].[SubscriberLastName]))) > 35 THEN [SubscriberLastName]
                 WHEN len(ltrim(Rtrim([sd].[SubscriberFirstName]))) > 15 THEN [SubscriberFirstName]
                 WHEN len(ltrim(Rtrim([sd].[SubscriberMiddleInitial]))) > 1 THEN [SubscriberMiddleInitial]  
                 WHEN len(ltrim(Rtrim([sd].[Sex]))) > 1 THEN [Sex]
                 WHEN len(ltrim(Rtrim([sd].[Dob]))) > 8 THEN [Dob]   
                 WHEN len(ltrim(Rtrim([sd].[Ssn]))) > 9 THEN [Ssn]
                 WHEN len(ltrim(Rtrim([sd].[FacetsGroup]))) > 8 THEN [FacetsGroup]  
           END AS ErrorValue,           
           @ActiveFlag AS [ActiveFlag]
           FROM [Eligibility].[SubscriberData]
           WHERE len(ltrim(Rtrim([sd].[SubscriberNumber]))) > 9 OR
                 len(ltrim(Rtrim([sd].[SubscriberLastName]))) > 35 OR
                 len(ltrim(Rtrim([sd].[SubscriberFirstName]))) > 15 OR
                 len(ltrim(Rtrim([sd].[SubscriberMiddleInitial]))) > 1 OR  
                 len(ltrim(Rtrim([sd].[Sex]))) > 1 OR
                 len(ltrim(Rtrim([sd].[Dob]))) > 8 OR   
                 len(ltrim(Rtrim([sd].[Ssn]))) > 9 OR
                 len(ltrim(Rtrim([sd].[FacetsGroup]))) > 8 

由于

1 个答案:

答案 0 :(得分:0)

理想情况下,应定义源表,以使每个字段不能大于定义的最大值。因此,我猜测(希望)在修复基础表定义之前,这是一次一次性的练习,以识别错误数据并修复它。

正如@Matt评论的那样,这里有很多len(ltrim(rtrim(,这会让这个变慢。

正如@Arvo指出的那样,您只会记录每行中的第一个错误 - 例如如果SubscriberNumberSubscriberLastName都太长,则只会记录SubscriberNumber

您应该确保在编写数据时修剪数据。这将消除整个商店ltrim(rtrim(的需要。如有必要,在表格中运行一次性查询以修复现有数据值。

您的where子句可能与它一样有效(一旦丢失所有这些修剪),并且可用于驱动游标或填充临时表,然后您可以多次查询 - 每次现场检查一次。

后一种解决方案可能如下所示。首先获取有错误的行:

SELECT
     [sd].[SuscriberDataId]
    ,[sd].[SubscriberNumber]
    ,[sd].[SubscriberLastName]
    ,[sd].[SubscriberFirstName]
    ,[sd].[SubscriberMiddleInitial]
    ,[sd].[Sex]
    ,[sd].[Dob]
    ,[sd].[Ssn]
    ,[sd].[FacetsGroup]
INTO #temp
FROM [Eligibility].[SubscriberData] [sd]
WHERE
    len([sd].[SubscriberNumber]) > 9 OR
    len([sd].[SubscriberLastName]) > 35 OR
    len([sd].[SubscriberFirstName]) > 15 OR
    len([sd].[SubscriberMiddleInitial]) > 1 OR  
    len([sd].[Sex]) > 1 OR
    len([sd].[Dob]) > 8 OR   
    len([sd].[Ssn]) > 9 OR
    len([sd].[FacetsGroup]) > 8 

然后,对于一次一个字段,获取违反规则的行:

-- SubscriberNumber check
INSERT INTO [Elig].[dbo].[ErrorTable]
    (
        [SeqId],
        [CodeId],
        [SubjectArea],
        [FieldName],
        [TableName],
        [ErrorValue],
        [ActiveFlag]
    )

SELECT
    [t].[SuscriberDataId],
    @InvalidLength,
    @SubjectArea,
    'SubscriberNumber',
    'Eligibility.SubscriberData',
    [t].[SubscriberNumber],
    @ActiveFlag
FROM #temp
WHERE len([t].[SubscriberNumber]))) > 9

重复下一个字段等