June 23, 2012 at 1:07 pm
Comments posted to this topic are about the item Script to Search Through all Databases for a Table Name
June 25, 2012 at 3:22 am
A similar result may be achieved using the undocumented stored procedure sp_MSforeachdb:
EXECUTE master.sys.sp_MSforeachdb 'USE [?]; SELECT table_catalog + ''.'' + table_schema + ''.'' + table_name from [?].INFORMATION_SCHEMA.TABLES where table_type = ''BASE TABLE'' AND TABLE_NAME LIKE ''%Sales.Cust%'''
June 25, 2012 at 7:58 am
Microsoft included a stored procedure with SQL Server 2000 called "sp_MSObjSearch" that did exactly what you're proposing to do. It's parameterized so that you can choose what type of object to search for.
If you have an instance of SQL 2000 available, you can get view the code by executing sp_helptext sp_MSObjSearch.
This procedure is incredibly helpful, and I'm mystified as to why MS stopped including it in the basic server installation. I've seen the procedure deployed successfully in SQL 2005 and SQL 2008 instances.
June 25, 2012 at 8:47 am
Thanks very much but I dont have sql 2000. I am using 2005 and 2008.
June 25, 2012 at 9:16 am
Would this not achieve the same thing?
DECLARE @sql NVARCHAR(MAX), @SEARCH_STRING NVARCHAR(100) = '%TB_DIM%';
SELECT @sql = STUFF(sqlCode.value('.', 'varchar(max)'),1,10,'') + ';'
FROM (SELECT 'UNION ALL SELECT table_catalog + '+CHAR(39)+'.'+CHAR(39)+' + table_name ' + CHAR(13) + CHAR(10) +
'FROM ' + QUOTENAME(Name) + '.INFORMATION_SCHEMA.TABLES '+ CHAR(13) + CHAR(10) +
'WHERE table_type = ' + CHAR(39) + 'BASE TABLE' + CHAR(39) + ' AND table_name LIKE ' +
CHAR(39) + @SEARCH_STRING + CHAR(39) + CHAR(13) + CHAR(10)
FROM sys.databases
WHERE database_id > 4
FOR XML PATH(''), TYPE) a(sqlCode);
PRINT @sql;
EXECUTE sp_executesql @sql;
June 25, 2012 at 4:08 pm
I really love a FREE tool - SQL Search from RED Gate - http://www.red-gate.com/products/sql-development/sql-search/.
It seaches pretty much any DB subject, table, view, store proc, columns, etc., very fast. Thought you maybe interested.
July 2, 2012 at 10:04 am
We just made a similar script but focusing on the columns and not using the schemas (as they don't use them in here).
But because we're not dbas and we don't have access to all databases, our query at sys.databases had a couple of extra filters to avoid errors.
AND state_desc = 'ONLINE'
AND HAS_DBACCESS (name) = 1
May 10, 2016 at 9:31 am
Thanks for the script.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply