Find table and field a particular value reside

There are times (when we create a report in Crystal or SSRS or run an update statement in SQL), we want to know in which table and field a particular value resides. It can be a tedious and boring process to sift through all the table & field suspects in the DB.

Let's take an example. I want to update all the CHECKBOOK ID fields for all the Customers in the DB. I want to run an UPDATE statement such as:

UPDATE [TABLE] SET [XXXXX] WHERE CHECKBOOKID = 'BANKOFNY' and CUSTNMBR ='ABCD001'

But I do not know what fields in the RM00100 needs to be updated.

This is what you do. Go to your TEST database, create a new CHECKBOOK ID (let's call it TESTCHECKBOOK, apply it to one customer and save it.

Then you create the following stored procedure on the SQL server under your TEST Database:

USE [YourDB]
GO

/****** Object:  StoredProcedure [dbo].[_SearchAllTables]    Script Date: 4/13/2017 4:43:27 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


--First run the create procedure below, then run EXEC SearchAllTables 'UNPOSTAP'

CREATE PROC [dbo].[_SearchAllTables]
(
    @SearchStr nvarchar(100)
)
AS
BEGIN

CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

    SET NOCOUNT ON

    DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
    SET  @TableName = ''
    SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

    WHILE @TableName IS NOT NULL
    BEGIN
        SET @ColumnName = ''
        SET @TableName =
        (
            SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
            FROM     INFORMATION_SCHEMA.TABLES
            WHERE         TABLE_TYPE = 'BASE TABLE'
                AND    QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
                AND    OBJECTPROPERTY(
                        OBJECT_ID(
                            QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                             ), 'IsMSShipped'
                               ) = 0
        )

        WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
        BEGIN
            SET @ColumnName =
            (
                SELECT MIN(QUOTENAME(COLUMN_NAME))
                FROM     INFORMATION_SCHEMA.COLUMNS
                WHERE         TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                    AND    TABLE_NAME    = PARSENAME(@TableName, 1)
                    AND    DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
                    AND    QUOTENAME(COLUMN_NAME) > @ColumnName
            )
    
            IF @ColumnName IS NOT NULL
            BEGIN
                INSERT INTO #Results
                EXEC
                (
                    'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630)
                    FROM ' + @TableName + ' (NOLOCK) ' +
                    ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
                )
            END
        END    
    END

    SELECT ColumnName, ColumnValue FROM #Results
END

GO


Right click and execute the above SP. In the value field, type your checkbook ID [TESTCHECKBOOK] and run it.

That will tell you what all tables it resides in and the name of the field(s) you need to update to accomplish the task..

Hope this helps.

Paul Chacko

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences