搜索的值我有表像下面如何通過多次迭代列在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_%')
感謝
是不是很容易,只需有一個EAV型表? '(recordID,columnID,value)',那麼它將是一個簡單的'select columnID,其中的值'%whatever%'' –
它是客戶端數據。每一列都用於診斷代碼,所以實際的列名稱是DIAG_01,DIAG_02 ...如此 – user176047