The database looks for the table where a string is located.
Method 1 occasionally goes wrong
[SQL]
-- Method 1
Declare @ sSearchContent varchar (100) = 'f6f89834-817c-47d7-a436-6484754111cb'
Bytes -----------------------------------------------------------------------------------------------
DECLARE @ Table TABLE (id int identity (100), columnName VARCHAR (100), tablename VARCHAR ))
DECLARE @ Line INT = 1
DECLARE @ Total INT
DECLARE @ SQL NVARCHAR (4000)
DECLARE @ columnName VARCHAR (100)
DECLARE @ TableName VARCHAR (100)
DECLARE @ num int
Insert into @ Table
Select. name, OBJECT_NAME (. object_id) AS tablename from sys. columns as a inner join sysobjects as B ON. object_id = B. id and B. xtype = 'U' AND system_type_id = 167
SELECT @ Total = COUNT (1) FROM @ Table
WHILE @ Line <= @ Total
BEGIN
SELECT @ columnName = columnName, @ TableName = tablename
FROM @ Table
Where id = @ Line
-- Set @ SQL = 'select @ a = count (*) from '+ @ TableName + 'where' + @ columnName +' = 'Liu ba '''
Set @ SQL = 'select @ a = count (*) from '+ @ TableName + 'where' + @ columnName +' = ''' + @ sSearchContent + ''''
Exec sp_executesql @ SQL, n' @ a int output', @ num output
IF @ num> 0
BEGIN
SELECT @ TableName AS TableName, @ columnName AS ColumnName
END
SET @ Line = @ Line + 1
END
Bytes -----------------------------------------------------------------------------------------------
-- Method 2
Declare @ str varchar (100)
Set @ str = 'f6f89834-817c-47d7-a436-6484754111cb'
Bytes ----------------------------------------------------------------------------------------------
Declare @ s varchar (8000)
Declare tb cursor local
Select 'if exists (select 1 from ['+ B. name + '] where [' +. name + '] like ''%' + @ str + '% '')
Print ''['+ B. name +']. ['+ a. name +'] '''
From syscolumns a join sysobjects B on a. id = B. id
Where B. xtype = 'U' and a. status> = 0
And a. xusertype in (175,239,231,167)
Open tb
Fetch next from tb into @ s
While @ fetch_status = 0
Begin
Exec (@ s)
Fetch next from tb into @ s
End
Close tb
Deallocate tb
Bytes ----------------------------------------------------------------------------------------------
Author: keenweiwei