Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Requête de recherche sur plusieurs colonnes d'une table ?

Essayez ce qui suit,

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)