Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Suchanfrage über mehrere Spalten einer Tabelle?

Versuchen Sie Folgendes,

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)