Domanda

I have table like below

CREATE TABLE [dbo].[SAMPLE](
[COL_01] [nvarchar](50) NULL,
[COL_02] [nvarchar](50) NULL,
[COL_03] [nvarchar](50) NULL,
[COL_04] [nvarchar](50) NULL,
[COL_05] [nvarchar](50) NULL,
[COL_06] [nvarchar](50) NULL,
[COL_07] [nvarchar](50) NULL,
[COL_08] [nvarchar](50) NULL,
[COL_09] [nvarchar](50) NULL,
[COL_10] [nvarchar](50) NULL,
[COL_11] [nvarchar](50) NULL,
[COL_12] [nvarchar](50) NULL,
[COL_13] [nvarchar](50) NULL,
[COL_14] [nvarchar](50) NULL,
[COL_15] [nvarchar](50) NULL,
[COL_16] [nvarchar](50) NULL,
[COL_17] [nvarchar](50) NULL,
[COL_18] [nvarchar](50) NULL
) ON [PRIMARY]

I want to search for a value like 'Unknown' in each of these columns. how do iterate through these 18 columns to find a value 'Unknown'. Currently I use something like below to generate a SQL but was wondering if there is cursor or stored procedure that can wrap this in

SELECT 'Select * from SAMPLE where '+ c.name + ' = ''Unknown'''
FROM sysobjects o
INNER JOIN syscolumns c ON c.id = o.id
INNER JOIN systypes t ON t.xusertype = c.xusertype
WHERE o.name = 'SAMPLE'
AND C.name like ( 'COL_%' )

Thanks

È stato utile?

Soluzione

You can unpivot your results and then compare against 'Unknown'. This is one way (for SQL Server 2008+):

SELECT
  x.Col,
  x.Value
FROM dbo.[SAMPLE] t
CROSS APPLY 
(
    VALUES
        ('COL_01', t.COL_01),
        ('COL_02', t.COL_02),
        ('COL_03', t.COL_03),
        ('COL_04', t.COL_04),
        ('COL_05', t.COL_05),
        ('COL_06', t.COL_06),
        ('COL_07', t.COL_07),
        ('COL_08', t.COL_08),
        ('COL_09', t.COL_09),
        ('COL_10', t.COL_10),
        ('COL_11', t.COL_11),
        ('COL_12', t.COL_12),
        ('COL_13', t.COL_13),
        ('COL_14', t.COL_14),
        ('COL_15', t.COL_15),
        ('COL_16', t.COL_16),
        ('COL_17', t.COL_17),
        ('COL_18', t.COL_18)
) x (Col, Value)
WHERE X.Value = 'Unknown';

For SQL Server 2005+, you can use the variation of the above where the VALUES ... syntax is replaced with a series of SELECTs combined via UNION ALL:

SELECT
  x.Col,
  x.Value
FROM dbo.[SAMPLE] t
CROSS APPLY 
(
    SELECT 'COL_01', t.COL_01 UNION ALL
    SELECT 'COL_02', t.COL_02 UNION ALL
    SELECT 'COL_03', t.COL_03 UNION ALL
    SELECT 'COL_04', t.COL_04 UNION ALL
    SELECT 'COL_05', t.COL_05 UNION ALL
    SELECT 'COL_06', t.COL_06 UNION ALL
    SELECT 'COL_07', t.COL_07 UNION ALL
    SELECT 'COL_08', t.COL_08 UNION ALL
    SELECT 'COL_09', t.COL_09 UNION ALL
    SELECT 'COL_10', t.COL_10 UNION ALL
    SELECT 'COL_11', t.COL_11 UNION ALL
    SELECT 'COL_12', t.COL_12 UNION ALL
    SELECT 'COL_13', t.COL_13 UNION ALL
    SELECT 'COL_14', t.COL_14 UNION ALL
    SELECT 'COL_15', t.COL_15 UNION ALL
    SELECT 'COL_16', t.COL_16 UNION ALL
    SELECT 'COL_17', t.COL_17 UNION ALL
    SELECT 'COL_18', t.COL_18
) x (Col, Value)
WHERE X.Value = 'Unknown';

Or you can use UNPIVOT:

SELECT *
FROM dbo.[SAMPLE] AS t
UNPIVOT(Value FOR Col IN (COL_01,COL_02,COL_03,COL_04.....COL_18)) AS x
WHERE x.Value = 'Unknown';
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top