Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

locate which database a view, table, sp belongs to 1

Status
Not open for further replies.

R7Dave

Programmer
Oct 31, 2007
181
US
Hello

In SQL2005, is there a tool or T-SQL statement I can run that will let me know what database a table, or view, stored procedure belongs to.

Thanks
Dave
 
Does this help?
Code:
DECLARE @ObjectToSearchFor VARCHAR(200)
SELECT @ObjectToSearchFor = 'NameOfSomeTable'

SELECT TOP 0 * INTO ##TablesAndViews FROM INFORMATION_SCHEMA.TABLES 
SELECT TOP 0 * INTO ##Routines FROM INFORMATION_SCHEMA.ROUTINES
	
EXEC Master..sp_msforeachdb 'INSERT INTO ##TablesAndViews SELECT * FROM ?.INFORMATION_SCHEMA.TABLES; INSERT INTO ##Routines SELECT * FROM ?.INFORMATION_SCHEMA.ROUTINES'

SELECT * FROM ##TablesAndViews WHERE TABLE_NAME = @ObjectToSearchFor 
SELECT * FROM ##Routines WHERE ROUTINE_NAME = @ObjectToSearchFor 
DROP TABLE ##TablesAndViews
DROP TABLE ##Routines
 
very nice - yes it does - thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top