SQL Query to find database name if you know only table name

This query is usefull when you wanted to know the database name by passing the table name.

Some situations you will not remember the database name but you remember the table name. I was thinking what we will do in such cases!!!

Don't worry Microsoft has solution for it.

You can use below query to find out the database name. Just replace the table name in place of tbl_login

exec master.dbo.sp_msforeachdb
"USE [?]
select db_name()
from sysobjects
where name = 'tbl_login'"


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: