Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Query di ricerca su più colonne di una tabella?

Prova quanto segue,

DECLARE @ColName varchar(126) = 'col_name'
DECLARE @ColVal varchar(64) = 'col_val'

DECLARE @SQL AS nvarchar(MAX)
SET @SQL = ''

SELECT @SQL = @SQL + 'SELECT * FROM ' + IST.TABLE_SCHEMA + '.' + IST.TABLE_NAME 
                       +' where '+ @ColName +' = '''+ @ColVal +''' ' + CHAR(13)
FROM 
    INFORMATION_SCHEMA.TABLES IST
    INNER JOIN INFORMATION_SCHEMA.COLUMNS ISC ON IST.TABLE_NAME = ISC.TABLE_NAME
WHERE
    ISC.COLUMN_NAME = @ColName

EXEC (@SQL)