Column Name

  • Hi,

    I have table which have number of columns one of these columns contains value say 'ABC' I have to find the column name which contain ABC

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • You can try the below however, this might take a long time depending on the number of records in the table.

    USE YOUR_DATABASE_NAME

    GO

    DECLARE @MY_COL VARCHAR(50)

    DECLARE @QRY VARCHAR (255)

    declare kursor cursor for

    SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'YOUR_TABLE_NAME'

    OPEN kursor

    FETCH NEXT FROM kursor INTO @MY_COL

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SET @QRY= 'SELECT '+@MY_COL+' FROM YOUR_TABLE_NAME WHERE '+@MY_COL+' =''ABC'''

    PRINT @QRY

    EXEC (@QRY)

    FETCH NEXT FROM kursor INTO @MY_COL

    END

    CLOSE kursor

    DEALLOCATE kursor

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply