This question already has answers here:
Select databases which only contain specific table (3 answers)
Closed 20 days ago.
I have many databases in my SQL Server.
I have to just search for database names containg particular table name Heartbitmaster
I have many databases such as Gotgold
, DVD
, etc and I just want to find database names from query that contain this table Heartbitmaster
.
I searched I tried for query:
SELECT
TABLE_NAME
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE = 'base table'
AND table_schema = 'Heartbitmaster'
but it didn't work.
I searched further and came across:
SELECT name, database_id, create_date
FROM sys.databases
but dont know how to arrange further where condition for search of table name
Please help me.
7条答案
按热度按时间kgsdhlau1#
I got it done through following query:
qq24tv8q2#
try this one
nzrxty8p3#
I needed something slightly different.
This will return all tables and their corresponding DBs with names containing the supplied string:
d7v8vwbk4#
If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??
As for the
INFORMATION_SCHEMA
or the SQL Server specific catalog views: as far as I know, those are always constrained to the current database you're in - so you cannot search across all databases on your server. SQL Search does this for you - by searching into every single database on the server.btqmn9zl5#
Create Procedure as bellow
dm7nw8vv6#
It Works!!!!!!!
Run this query for finding database name for a particular table The paste table name in @tablename
Drop table #tempo for next time run
At the last giving error like
Database 'databasename' cannot be opened because it is offline.
then run this query separately
crcmnpdw7#
I Complete sp_MSforeachdb and create a procedure like this :