SQL获取返回的每个记录的空/空字段

时间:2016-07-05 15:08:22

标签: sql sql-server sql-server-2012

我有以下查询,我用它来检索至少有以下列之一设置为''DECLARE @missingFields varchar(100) SET @missingFields = '' SELECT c.[UnitReference] as 'UnitRef', t.[NodeName] as 'Property', @missingFields as 'Field/s missing' FROM [DetailPage] d INNER JOIN [CRM] c ON d.ItemID = c.ItemID INNER JOIN [Tree] t ON d.[ID] = t.[ID] WHERE (ISNULL(d.MainImage, '') = '' OR ISNULL(d.Summary,'') = '' OR ISNULL(d.[Description],'') = '') AND c.[IsListed] = 1 AND c.[IsMarketed] = 1 的记录:

  • MainImage
  • 摘要
  • 描述

    "Main Image is empty, Description is empty"

这将返回我想要的数据,但是我还需要构建一个字符串,列出返回记录的哪些列为空或为空,例如当记录有空MainImageDescription列时,@missingFields = CASE WHEN ISNULL(MainImage, '') = '' THEN 'Main image is null' ELSE '' END -- etc...

我试过了:

{{1}}

但我无法将此包含在数据检索操作中。我该怎么做呢?

1 个答案:

答案 0 :(得分:2)

    SELECT c.[UnitReference] AS 'UnitRef',
           t.[NodeName] AS 'Property',
           ( CASE
                 WHEN d.MainImage IS NULL
                 THEN 'Main Image is Null, '
                 WHEN d.MainImage = ''
                 THEN 'Main Image is Empty, '
                 ELSE ''
             END )+
             ( CASE
                WHEN d.Summary IS NULL
                THEN 'Summary is Null, '
                WHEN d.Summary = ''
                THEN 'Summary is Empty, '
                ELSE ''
            END )+
            ( CASE
                WHEN d.[Description] IS NULL
                THEN 'Description is Null, '
                WHEN d.[Description] = ''
                THEN 'Description is Empty, '
                ELSE ''
            END ) AS MissingFields
    FROM [DetailPage] d
    INNER JOIN [CRM] c
    ON d.ItemID = c.ItemID
    INNER JOIN [Tree] t
    ON d.[ID] = t.[ID]
    WHERE( d.MainImage IS NULL OR d.MainImage = '' )
     OR ( d.Summary IS NULL OR d.Summary = '' )
     OR ( d.[Description] IS NULL OR d.[Description] = '' )
     AND c.[IsListed] = 1
     AND c.[IsMarketed] = 1