The database looks for the table where a string is located.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.