—
— Dynamically builds T-SQL statements for retrieving all the records
— from all the tables in all user databases
—
— SQL Server versions supported: SQL Server 2000 or later
—
exec sp_MSforeachdb
@command1=”IF DB_ID(‘?’) > 4 print ‘use ?;'”,
@command2=”IF DB_ID(‘?’) > 4 SELECT ‘SELECT * FROM [‘+TABLE_SCHEMA+’.’+TABLE_NAME+’]’ FROM ?.INFORMATION_SCHEMA.TABLES”
Details:
- In Query Window in SSMS, right click – Results To: Results to Text
- Run the above query and you will get “SELECT *” statements for each column of each table in each user database (Database ID > 4). By executing the produced code you get all the data records.
- Undocumented stored procedure sp_MSforeachdb
- Information Schema Views
Artemakis Artemiou, a distinguished Senior Database and Software Architect, brings over 20 years of expertise to the IT industry. A Certified Database, Cloud, and AI professional, he earned the Microsoft Data Platform MVP title for nine consecutive years (2009-2018). As the founder of SQLNetHub and GnoelixiAI Hub, Artemakis is dedicated to sharing his knowledge and democratizing education on various fields such as: Databases, Cloud, AI, and Software Development. His commitment to simplicity and knowledge sharing defines his impactful presence in the tech community.