Reputation:
I want to find a table in SQL Server, let's say "XYZ". i don't know in which DB it is located. The server has many DBs
Upvotes: 0
Views: 52
Reputation: 2104
This query will return a listing of all tables in all databases on a SQL instance:
DECLARE @command varchar(1000)
SELECT @command = 'USE ? SELECT name FROM sysobjects WHERE xtype = ''U'' AND Name LIKE ''%A'' ORDER BY name'
EXEC sp_MSforeachdb @command
Upvotes: 0
Reputation: 5947
SELECT * FROM sys.Tables
WHERE
name LIKE '%XYZ%'
You can make use of SP sp_Msforeachdb
- this will be executed on every single database you have in the server.
EXEC sp_Msforeachdb "USE [?]; SELECT '[?]' dbname, *
FROM sys.tables
WHERE name like '%XYA%'"
Upvotes: 3