—
— 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 is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.