March 16, 2025 at 4:48 pm
When I log into my instance of SQL Server, there are many databases. I'm trying to find a specific view, but I don't know which database it is in.
I have used
Select * FROM INFORMATION_SCHEMA.VIEWS but that is when I know the database name. We have more than 20 databases in my instance.
Is there a way to search all the databases to find a specific "view"?
Thank you,
JP
March 16, 2025 at 7:37 pm
This may give you an idea... I dumped the result of the query into a temporary table. (Not sure how to parameterize the object name, for some reason.)
CREATE TABLE #DBList(dbname NVARCHAR(50));
GO
DECLARE @command2 VARCHAR(1000)
SELECT @command2 = 'USE ? select DB_NAME(DB_ID(''?'')) FROM sysobjects where xtype = ''V'' AND name LIKE ''vwB%'' ';
INSERT INTO #DBList(dbname)
exec sp_msforeachdb @command2;
SELECT *
FROM #DBList;
March 16, 2025 at 7:37 pm
This may give you an idea... I dumped the result of the query into a temporary table. (Not sure how to parameterize the object name, for some reason.)
CREATE TABLE #DBList(dbname NVARCHAR(50));
GO
DECLARE @command2 VARCHAR(1000)
SELECT @command2 = 'USE ? select DB_NAME(DB_ID(''?'')) FROM sysobjects where xtype = ''V'' AND name LIKE ''vwB%'' ';
INSERT INTO #DBList(dbname)
exec sp_msforeachdb @command2;
SELECT *
FROM #DBList;
March 17, 2025 at 12:15 am
Humm... I couldn't get it to work. You gave: 'USE ? select DB_NAME(DB_ID(''?''))
But I don't know the database name. The server has many databases. I want to search all the databases in the server to find a specific "view". Please, what am I missing?
Thanks,
JP
March 17, 2025 at 1:59 am
the sp_foreachdb part?
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy