Yesterday I wrote about SQL SERVER – Find Table in Every Database of SQL Server. Today we will see another method how we can achieve the same result using Information_Schema view. Refer my previous article here for additional information.
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 (TABLE_CATALOG VARCHAR(128), TABLE_SCHEMA VARCHAR(128), TABLE_NAME VARCHAR(256), TABLE_TYPE VARCHAR(10))
OPEN @getDBName
FETCH NEXT
FROM @getDBName INTO @DBName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @varSQL = 'USE ' + @DBName + ';
INSERT INTO #TmpTable
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_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
Reference : Pinal Dave (http://blog.SQLAuthority.com)
SQL SERVER – Find Table in Every Database of SQL Server – Part 2
April 30, 2008 by pinaldave
Posted in Pinal Dave, SQL, SQL Authority, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology | No Comments Yet
Leave a Reply
-
About Pinal Dave
Pinalkumar Dave is Microsoft SQL Server MVP, Solid Quality Mentor and a prominent author of over 1000 SQL Server articles at SQLAuthority. He is a dynamic and proficient Principal Database Architect, Corporate Trainer and Project Manager specializing in SQL Server Programming with over 7 years of hands-on experience. He holds a degree in Masters of Science and has accomplished a number of certifications including MCDBA and MCAD (.NET). He has also been awarded Regional Mentor for PASS Asia.
-
Blog Stats
- 8,492,636 Readers
-
SQLAuthority Links

My Homepage
My Resume
My Other Blog
--------------------
Top Downloads
PDF Downloads
Script Downloads
Script Bank
Favorite Scripts
All Scripts - 1
All Scripts - 2
Top Articles
Best Articles
Favorite Articles - 1
Favorite Articles - 2
--------------------
SQL Interview Q & A
SQL Coding Standards
SQL FAQ Download
--------------------
Jobs @ SQLAuthority
Categories
- About Me (50)
- Best Practices (82)
- Business Intelligence (6)
- Data Warehousing (27)
- Database (257)
- DBA (121)
- DigiCorp (7)
- MVP (82)
- Poll (5)
- Readers Contribution (22)
- Readers Question (29)
- Software Development (62)
- SQL Add-On (88)
- SQL Backup and Restore (51)
- SQL BOL (8)
- SQL Coding Standards (21)
- SQL Constraint and Keys (49)
- SQL Cursor (30)
- SQL Data Storage (39)
- SQL DateTime (36)
- SQL Documentation (193)
- SQL Download (198)
- SQL Error Messages (116)
- SQL Function (104)
- SQL Humor (22)
- SQL Index (82)
- SQL Interview Questions and Answers (54)
- SQL Joins (61)
- SQL Optimization (51)
- SQL Performance (204)
- SQL Puzzle (18)
- SQL Security (115)
- SQL Server DBCC (42)
- SQL Server Management Studio (17)
- SQL Stored Procedure (97)
- SQL String (17)
- SQL System Table (27)
- SQL Trigger (27)
- SQL User Group (41)
- SQL Utility (116)
- SQL White Papers (8)
- SQLAuthority (309)
- SQLAuthority Author Visit (62)
- SQLAuthority Book Review (19)
- SQLAuthority News (279)
- SQLAuthority Website Review (24)
- SQLServer (44)
- Tech (778)
- Pinal Dave (767)
- SQL Scripts (491)
- Technology (1029)
- SQL (1029)
- SQL Authority (1029)
- SQL Query (1029)
- SQL Server (1029)
- SQL Tips and Tricks (1029)
- T SQL (1029)
-
Top Posts
- SQL SERVER - Insert Data From One Table to Another Table - INSERT INTO SELECT - SELECT INTO TABLE
- SQL SERVER - Insert Multiple Records Using One Insert Statement - Use of UNION ALL
- SQL SERVER - Retrieve Current Date Time in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}
- SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server
- SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects
- SQL SERVER - Convert Text to Numbers (Integer) - CAST and CONVERT
- SQL Server Interview Questions and Answers Complete List Download
- SQL SERVER - Shrinking Truncate Log File - Log Full
- SQL SERVER - Restore Database Backup using SQL Script (T-SQL)
- SQL SERVER - 2008 - Interview Questions and Answers Complete List Download
- SQL SERVER - 2005 List All Tables of Database
- SQL SERVER - 2005 NorthWind Database or AdventureWorks Database - Samples Databases
-
Authors
-
pinaldave
- SQL SERVER – Restore Sequence and Understanding NORECOVERY and RECOVERY
- SQL SERVER – Backup Timeline and Understanding of Database Restore Process in Full Recovery Model
- SQL SERVER – BLOB – Pointer to Image, Image in Database, FILESTREAM Storage
- SQLAuthority News – Big Thinkers – Robert Cain
- SQL SERVER – Standby Servers and Types of Standby Servers
- SQLAuthority News – Request SQLAuthority.com Stickers and SQL Server Cheat Sheet
- SQLAuthority News – Authors Visit – K-MUG TechEd Trivandrum on June 27, 2009
- SQLAuthority News – Book Review – Murach’s SQL Server 2008 for Developers
- SQLAuthority News – Authors Visit – DotNet Buzz Delhi TechEd Delhi on July 11, 2009
- SQL SERVER – Languages for BI – MDX, DMX, XMLA
-
Archives
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
Pages
Category Cloud
About Me Best Practices Database DBA MVP Pinal Dave Software Development SQL SQL Add-On SQL Authority SQLAuthority Author Visit SQLAuthority News SQL Documentation SQL Download SQL Error Messages SQL Function SQL Index SQL Interview Questions and Answers SQL Joins SQL Optimization SQL Performance SQL Query SQL Scripts SQL Security SQL Server SQL Stored Procedure SQL Tips and Tricks SQL Utility Technology T SQL


