SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SQL Syntax return TABLE NAMES by column VALUE
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

nferri
Starting Member

USA
5 Posts

Posted - 11/21/2012 :  12:40:35  Show Profile  Reply with Quote
Hi, I have a database where every table has an id column (CID) once this value changes all the tables must be checked and updated with the new CID value. I would like a write a query that returns all table names that DO or DO NOT have "CID = x" where x is the current CID value I define through a parameter. This way i can check and see what tables still need to be updated. Please help. Thanks.

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/21/2012 :  12:49:51  Show Profile  Reply with Quote
-- if 9455 is the CID you want to test for
CREATE TABLE #tmp(tablename VARCHAR(255), UnmatchedCID VARCHAR(32));
INSERT INTO #tmp 
EXEC sp_msforeachtable 
	'select top 1 ''?'' as tablename, ''YES'' UnmatchedCID from ? where CID <> 9455'
INSERT INTO #tmp 
EXEC sp_msforeachtable 
	'select top 1 ''?'' as tablename, ''NO'' UnmatchedCID from ? where CID = 9455'
SELECT * FROM #tmp
DROP TABLE #tmp;
Go to Top of Page

nferri
Starting Member

USA
5 Posts

Posted - 11/22/2012 :  10:04:54  Show Profile  Reply with Quote
THANKS!!!!!! perfect
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/22/2012 :  10:28:14  Show Profile  Reply with Quote
You are very welcome.)
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22713 Posts

Posted - 11/29/2012 :  06:26:27  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Why do you want to change it for all tables?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000