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 4, 2012 2:48 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:27 AM
Points: 1,890, Visits: 2,329
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/
Post #1392307
Posted Tuesday, December 4, 2012 3:09 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 24, 2014 11:46 PM
Points: 42, Visits: 290
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