SQL Query to find tables in the Database

Our server has around 10 databases.

On giving a Table Name or view or Stored Procedure, How to find which database this table belongs ??

Some thing similar to Sys.objects, Do we have in the Server Level.

Thanks
Chokka
chokkaStudentAsked:
Who is Participating?
 
dsackerContract ERP Admin/ConsultantCommented:
This requires a bit of dynamic SQL. You might like this:
SET NOCOUNT ON

DECLARE @db             varchar(80),
        @sql            varchar(1000),
        @objname        varchar(80)

SET     @objname = '$(obj)'
SET @ObjName = 'myTable'   -- comment this out if you want to use it in SQLCMD

DECLARE @Results TABLE (
    objname varchar(80) NOT NULL,
    db      varchar(80) NOT NULL,
    type    varchar(80) NOT NULL  )

DECLARE csr CURSOR LOCAL FAST_FORWARD FOR
SELECT name FROM sys.databases ORDER BY name

OPEN csr

WHILE 1 = 1
BEGIN
    FETCH FROM csr INTO @db
    IF @@FETCH_STATUS <> 0 BREAK

    SET @sql = 'USE ' + @db + '; ' +
               'IF OBJECT_ID(''' + @objname + ''') IS NOT NULL ' +
               'SELECT ''' + @objname + ''', ' +
                      '''' + @db + ''', ' +
                            'type_desc ' +
                 'FROM sys.objects ' +
                'WHERE name = ''' + @objname + ''''

    INSERT INTO @Results
    EXEC (@sql)
END

CLOSE csr
DEALLOCATE csr

SELECT * FROM @Results

SET NOCOUNT OFF

Open in new window

0
 
chokkaStudentAuthor Commented:
Thank you !! I will test the query and update the comment.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
chokka, do you still need help with this question?
0
 
chokkaStudentAuthor Commented:
Thank you !!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.