Here’s a quick T-SQL tip on getting the paths for all database files in a SQL Server instance.
The T-SQL Tip
You can get the list of paths for all database files (both data and log files) using the undocumented SQL Server stored procedure sp_MSforeachdb. You can use the below T-SQL statement:
exec sp_MSforeachdb @command1="USE [?]; SELECT CAST(DB_NAME() AS VARCHAR(50)) AS DBName, CAST(physical_name AS VARCHAR(150)) as Files FROM sys.database_files";
For more info about the undocumented SQL Server stored procedure sp_MSforeachdb, you can check this article on my SQLNetHub.
Strengthen your SQL Server Administration Skills – Enroll to our Online Course!
Check our online course on Udemy titled “Essential SQL Server Administration Tips”
(special limited-time discount included in link).Via the course, you will learn essential hands-on SQL Server Administration tips on SQL Server maintenance, security, performance, integration, error handling and more. Many live demonstrations and downloadable resources included!
Related T-SQL Tips on SQLNetHub
- Script that Returns SQL Server’s Version Name
- Executing T-SQL Statements Against All Databases
- Listing all Tables of a Linked Server’s Database
- Inserting Leading Characters to a String
- Retrieving Security-Related Info for SQL Server Logins
- Getting all the Records from all Tables in all User Databases
- …more
Featured Online Courses:
- SQL Server 2022: What’s New – New and Enhanced Features
- Introduction to Azure Database for MySQL
- Working with Python on Windows and SQL Server Databases
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- A Guide on How to Start and Monetize a Successful Blog
- Data Management for Beginners – Main Principles
Recommended Software Tools
Snippets Generator: Create and modify T-SQL snippets for use in SQL Management Studio, fast, easy and efficiently.
Dynamic SQL Generator: Convert static T-SQL code to dynamic and vice versa, easily and fast.
Subscribe to our newsletter and stay up to date!
Subscribe to our YouTube channel (SQLNetHub TV)
Check out our latest software releases!
Check out our eBooks!
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
© SQLNetHub
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.