2014-04-24 149 views
1

搜索的值我有表像下面如何通過多次迭代列在SQL服務器

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] 

我要搜索就像每個列的「未知」的值。如何遍歷這18列以找到「未知」值。目前我使用類似下面來生成SQL,但不知道是否有遊標或存儲過程可以在

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_%') 

感謝

+0

是不是很容易,只需有一個EAV型表? '(recordID,columnID,value)',那麼它將是一個簡單的'select columnID,其中的值'%whatever%'' –

+0

它是客戶端數據。每一列都用於診斷代碼,所以實際的列名稱是DIAG_01,DIAG_02 ...如此 – user176047

回答

8

把這個包您可以UNPIVOT您的結果,然後比較反對'Unknown'。這是(用於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'; 

對於SQL Server 2005 +,你可以用的上面對VALUES ...語法替換了一系列的SELECT通過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'; 

或者你可以使用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'; 
+1

我們是否應該提供處理「不到好」數據庫設計的技術?相反,只提供積極的選擇?我們長遠來說會更好嗎? –

+0

@RyanVincent我不知道你的意思。 「UNPIVOT」是爲這種桌子設計而明確創建的 – Lamak