Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Column Name Expand / Collapse
Author
Message
Posted Tuesday, December 04, 2012 2:48 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 12:53 AM
Points: 1,130, Visits: 1,276
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
Post #1392307
Posted Tuesday, December 04, 2012 3:09 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, May 03, 2013 3:06 AM
Points: 42, Visits: 285
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
Post #1392319
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse