SQL Server Display all the names of databases containing particular table [duplicate]

uklbhaso  于 9个月前  发布在  其他
关注(0)|答案(7)|浏览(92)

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.

kgsdhlau

kgsdhlau1#

I got it done through following query:

SELECT name
FROM sys.databases
WHERE  CASE WHEN state_desc = 'ONLINE' 
         THEN OBJECT_ID(QUOTENAME(name) + '.[dbo].[heartbit]', 'U')
       END IS NOT NULL
qq24tv8q

qq24tv8q2#

sp_MSforeachdb 'SELECT "?" AS DB, * FROM [?].sys.tables WHERE name like ''%tablename%'''

try this one

nzrxty8p

nzrxty8p3#

I needed something slightly different.

This will return all tables and their corresponding DBs with names containing the supplied string:

SELECT TABLE_NAME, TABLE_SCHEMA 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_NAME like '%_<insert_name_here>';
d7v8vwbk

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.

btqmn9zl

btqmn9zl5#

Create Procedure as bellow

CREATE PROCEDURE usp_FindTableNameInAllDatabase
@TableName VARCHAR(256)
AS
DECLARE @DBName VARCHAR(256)
DECLARE @varSQL VARCHAR(512)
DECLARE @getDBName CURSOR
SET @getDBName = CURSOR FOR
SELECT name
FROM sys.databases
CREATE TABLE #TmpTable (DBName VARCHAR(256),
SchemaName VARCHAR(256),
TableName VARCHAR(256))
OPEN @getDBName
FETCH NEXT
FROM @getDBName INTO @DBName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @varSQL = 'USE ' + @DBName + ';
INSERT INTO #TmpTable
SELECT '''+ @DBName + ''' AS DBName,
SCHEMA_NAME(schema_id) AS SchemaName,
name AS TableName
FROM sys.tables
WHERE name LIKE ''%' + @TableName + '%'''
EXEC (@varSQL)
FETCH NEXT
FROM @getDBName INTO @DBName
END
CLOSE @getDBName
DEALLOCATE @getDBName
SELECT *
FROM #TmpTable
DROP TABLE #TmpTable
GO
EXEC usp_FindTableNameInAllDatabase 'Address'
GO 

exec usp_FindTableNameInAllDatabase 'user'
dm7nw8vv

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

declare @tablename varchar(max) = 'patient'
declare @count int = (select max(database_id) FROM   sys.databases)
declare @n int = 1
declare @dbname varchar(max)
declare @query nvarchar(max)
create table #tempo(Databasename varchar(max), tablename varchar(max))
while @n <= @count
begin
select @dbname = name from sys.databases where database_id = @n and service_broker_guid <> '00000000-0000-0000-0000-000000000000'
set @query = 'insert into #tempo(Databasename,tablename) select '''+@dbname+''' [Database],name from '+@dbname+'.sys.tables where name like ''%'+@tablename+'%'''
exec(@query)
set @n=@n+1;
end
select * from #tempo

At the last giving error like

Database 'databasename' cannot be opened because it is offline.

then run this query separately

select * from #tempo
crcmnpdw

crcmnpdw7#

I Complete sp_MSforeachdb and create a procedure like this :

create PROCEDURE findTableName
@tablename nvarchar(max) 
AS
BEGIN
    declare @sqlCommand nvarchar(max) 
    CREATE TABLE #t (DBName VARCHAR(256),SchemaName VARCHAR(256),TableName VARCHAR(256))
    set @sqlCommand='insert into #t (DBName,SchemaName,TableName) exec sp_MSforeachdb ''SELECT "?" AS DB, SCHEMA_NAME(schema_id) AS SchemaName ,name FROM [?].sys.tables WHERE name like ''''%'+@tablename+'%'''''''
    --print @sqlCommand
    exec ( @sqlCommand)
    select * from #t
END

相关问题