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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.