Kristina
Kristina

Reputation: 699

How to check if specific string exists in columns of different tables? (Microsoft SQL Server)

I have 6 different tables, say: dbo.One, dbo.Two, dbo.Three,.....,dbo.Six

I want to check if a certain string (ex: "CatWS_Energy_CB") exists in ANY of the columns of the 6 different tables (This string is not the whole column name but is contained in the column name). Is there a way to do this? I'm trying to do an if statement in SQL but I'm not quite sure how to approach this first part.

Any help will be appreciated!

Upvotes: 1

Views: 1788

Answers (1)

Martin Smith
Martin Smith

Reputation: 453608

Based on discussion in the comments I think this is what you need.

SELECT Object_name(object_id, Db_id('Catastrophe'))
FROM   Catastrophe.sys.columns
WHERE  name LIKE '%CatWS_Energy_CB%'
       AND object_id IN ( Object_id('Catastrophe.dbo.One'), 
                          Object_id('Catastrophe.dbo.Two'), 
                          Object_id('Catastrophe.dbo.Three') ) 

Upvotes: 1

Related Questions