It's common problem which we face while working with SQL Server, sometimes we remembers the object name (fully or partially), but wanted to know in which DB that object occured, or sometime we want how many occurrence exists for that particular object in all databases. So stuck with this...here is solution:
--------FOR SQL SERVER 2005 or more ---------
CREATE TABLE #TEMP (DATABASENAME SYSNAME, OBJECTNAME SYSNAME,TYPE CHAR(10))
INSERT INTO #TEMP
EXEC SP_MSFOREACHDB "SELECT '?' DATABASENAME, NAME,TYPE FROM ?.SYS.ALL_OBJECTS WHERE NAME = 'SearchText'"
SELECT * FROM #TEMP
DROP TABLE #TEMP
Pass your object name in place of SearchText, above query will show all the object with such name, but what if we know only a part of object name...in such cases SQL Server's Like operator will be handy. We can use modify Where Clause as:
EXEC SP_MSFOREACHDB "SELECT '?' DATABASENAME, NAME,TYPE FROM ?.SYS.ALL_OBJECTS WHERE Like '%SearchText%'"
And it will show all the objects that contains SearchText.
For SQL Server 2005 or more it will work like charm. For user of SQL Server 2000 modified script as:
-----------FOR SQL SERVER 2000-----------------------------
CREATE TABLE #TEMP (DATABASENAME SYSNAME, OBJECTNAME SYSNAME,TYPE CHAR(10))
INSERT INTO #TEMP
EXEC SP_MSFOREACHDB "SELECT '?' DATABASENAME, NAME,XTYPE FROM ?..SYSOBJECTS WHERE NAME Like '%SearchText%'"
SELECT * FROM #TEMP
DROP TABLE #TEMP
Hope it helps..:)
--------FOR SQL SERVER 2005 or more ---------
CREATE TABLE #TEMP (DATABASENAME SYSNAME, OBJECTNAME SYSNAME,TYPE CHAR(10))
INSERT INTO #TEMP
EXEC SP_MSFOREACHDB "SELECT '?' DATABASENAME, NAME,TYPE FROM ?.SYS.ALL_OBJECTS WHERE NAME = 'SearchText'"
SELECT * FROM #TEMP
DROP TABLE #TEMP
Pass your object name in place of SearchText, above query will show all the object with such name, but what if we know only a part of object name...in such cases SQL Server's Like operator will be handy. We can use modify Where Clause as:
EXEC SP_MSFOREACHDB "SELECT '?' DATABASENAME, NAME,TYPE FROM ?.SYS.ALL_OBJECTS WHERE Like '%SearchText%'"
And it will show all the objects that contains SearchText.
For SQL Server 2005 or more it will work like charm. For user of SQL Server 2000 modified script as:
-----------FOR SQL SERVER 2000-----------------------------
CREATE TABLE #TEMP (DATABASENAME SYSNAME, OBJECTNAME SYSNAME,TYPE CHAR(10))
INSERT INTO #TEMP
EXEC SP_MSFOREACHDB "SELECT '?' DATABASENAME, NAME,XTYPE FROM ?..SYSOBJECTS WHERE NAME Like '%SearchText%'"
SELECT * FROM #TEMP
DROP TABLE #TEMP
Hope it helps..:)
Comments
Post a Comment