Query where a field in the SQL Server database is located

Source: Internet
Author: User
Sometimes we want to know which table and field the value comes from in the database through a value, and search for it online to find a better method, which is implemented through a stored procedure. You only need to input a value to query the table and field name of the value. The premise is to store the stored procedure in the queried database. CREATE

Sometimes we want to know which table and field the value comes from in the database through a value, and search for it online to find a better method, which is implemented through a stored procedure. You only need to input a value to query the table and field name of the value. The premise is to store the stored procedure in the queried database. CREATE

Sometimes we want to know which table and field the value comes from in the database through a value, and search for it online to find a better method, which is implemented through a stored procedure. You only need to input a value you want to search for to query the table and field name of the value.

The premise is to store the stored procedure in the queried database.

Create procedure [dbo]. [SP_FindValueInDB]

(

@ Value VARCHAR (1024)

)

AS

BEGIN

-- Set nocount on added to prevent extra result sets from

-- Interfering with SELECT statements.

Set nocount on;

DECLARE @ SQL VARCHAR (1024)

DECLARE @ table VARCHAR (64)

DECLARE @ column VARCHAR (64)

Create table # t (

Tablename VARCHAR (64 ),

Columnname VARCHAR (64)

)

DECLARE TABLES CURSOR

FOR

SELECT o. name, c. name

FROM syscolumns c

Inner join sysobjects o ON c. id = o. id

WHERE o. type = 'U' AND c. xtype IN (167,175,231,239)

Order by o. name, c. name

OPEN TABLES

FETCH NEXT FROM TABLES

INTO @ table, @ column

WHILE @ FETCH_STATUS = 0

BEGIN

SET @ SQL = 'if EXISTS (SELECT NULL FROM ['+ @ table +']'

SET @ SQL = @ SQL + 'where RTRIM (LTRIM (['+ @ column +']) LIKE ''% '+ @ value +' % '')'

SET @ SQL = @ SQL + 'insert INTO # t VALUES (''' + @ table + ''','''

SET @ SQL = @ SQL + @ column + ''')'

EXEC (@ SQL)

FETCH NEXT FROM TABLES

INTO @ table, @ column

END

CLOSE TABLES

DEALLOCATE TABLES

SELECT *

FROM # t

Drop table # t

End

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.