How can I write a query to return all columns of, for example, the sys.database_files
table? I see many suggestions for using information_schema.columns for getting table names for user databases/tables, but i can't figure out how to do this for system tables.
I see that the sys.database_files is a view under the master table. I'm using SQL Server 2012.
Best Answer
Since you are on SQL Server 2012, best is to use the new DMF
sys.dm_exec_describe_first_result_set
. This dmf will take a transact-SQL statement as a parameter and describes the metadata of the first result set for the statement.so for example, you want to find out the columns names along with the datatypes and precison for
sys.database_files
, simply useBelow are the results :
For versions below sql 2012:
Excellent post is written by Aaron Bertrand -- SQL Server v.Next (Denali) :Metadata enhancements which explains this in much details. Also, refer to the CAVEATS in the section down the article.
Avoid using INFORMATION_SCHEMA views. Worth reading Aaron's post on The case against INFORMATION_SCHEMA views.