0

I have a list of over 200 databases objects. All of these are either tables, stored procedures and views.

All of these objects exist on a specific SQL Server, but I was not given a field specifying which database each object belong in.

Given a list of DB objects that exists somewhere on a specific server, how can I query the server to provide the containing database name for each object?

6
  • What if objects of the same name exist in more than one database on the server? Commented Sep 18, 2014 at 19:04
  • @TabAlleman, in such a case, I would would an output of both objects, and their respective databases. In other words, if the object name is not unique, I'd like to see each database that the object exists in. This should be the case with this requirement. Regardless, if it happens, I just need to know where to find all of these objects. Commented Sep 18, 2014 at 19:42
  • I would go back to the person who created the nearly useless list and ask them which database it came from. And if it came from multiples I would ask to recreate the list with enough information to make it useful. Commented Sep 18, 2014 at 19:47
  • @SeanLange That would be nice but this data was scraped from Java code files, not SQL Server. "Go back to them, and throw it in their face" may seem like a nice approach, however, it isn't realistically an option in my case. Commented Sep 18, 2014 at 19:49
  • 1
    In that case I would use the code and look at the connection. It might be a little slower but it would be spot on accurate. Commented Sep 18, 2014 at 19:52

2 Answers 2

1

I had a similar issue, this is what worked for me:

-- List of objects .. store in a table somewhere with
-- a db column set to an empty string
create table tempdb.dbo._mylist ( name nvarchar(500), db nvarchar(500) )
insert tempdb.dbo._mylist values ('obj 1', '')
insert tempdb.dbo._mylist values ('obj 2', '')

-- Setup cursor for databases
DECLARE db_cursor CURSOR FOR
   SELECT name from sys.databases WHERE [state] != 6 -- OFFLINE

-- Loop through cursor
OPEN db_cursor;
DECLARE @dbname sysname;
FETCH NEXT FROM db_cursor INTO @dbname;
WHILE (@@FETCH_STATUS <> -1)
BEGIN;
    -- added dbname to object list if found (joined using common collation)
   EXECUTE ('use ' + @dbname + '; update l set db = db + '';' + @dbname + ''' from tempdb.dbo._mylist l join sysobjects o on o.name = l.name COLLATE SQL_Latin1_General_CP1_CI_AS;');
   FETCH NEXT FROM db_cursor INTO @dbname;
END;
CLOSE db_cursor;
DEALLOCATE db_cursor;
GO

-- Select results
select name, db = isnull(stuff(db,1,1,''), '') from tempdb.dbo._mylist

-- Cleanup
drop table  tempdb.dbo._mylist
Sign up to request clarification or add additional context in comments.

Comments

0

You can write a script using the SP_MSFOREACHDB stored procedure to do this. You can find examples of this here This basically allows you to run a script against all the databases.

For Example, the statement below will allow you to search for a table name, and it will also return the associated databasename.

  EXEC sp_Msforeachdb "USE [?]; SELECT '[?]' databaseName, * FROM sys.tables WHERE name = 'table_name'"

1 Comment

That undocumented procedure has some serious problems with it. Take a look here for a better alternative. sqlblog.org/2018/10/22/sp-ineachdb-2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.