我正在使用此代码段在数据库的任何表中查找特定字符串!
DECLARE @searchstring NVARCHAR(255)
SET @searchstring = '%foo%'
DECLARE @sql NVARCHAR(max)
SELECT @sql = STUFF((
SELECT ' UNION ALL SELECT ' + COLUMN_NAME + ' ' +
' FROM ' + TABLE_NAME +
' WHERE ' + COLUMN_NAME + ' LIKE ''' + @searchstring + ''''
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE in ('nvarchar', 'varchar', 'char','nchar')
FOR XML PATH('')
) ,1, 11, '')
exec (@sql)
但我想获得这些条目的主键值!
要获取我使用的特定表的主键
SELECT ccu.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu ON tc.CONSTRAINT_NAME = ccu.Constraint_name
WHERE tc.CONSTRAINT_TYPE = 'Primary Key'
AND tc.TABLE_NAME = 'tbl_table'
但我不能将两者结合起来?!