Best practice when getting the structure of the resulting query or table.
Use the SET FMTONLY ON and OFF option
There are several instances when we require the structure of the query or the table. One of the most common practices to achieve this is to use "
WHERE 1=0
" along with the select
part of the statement. It's really not practical when the query has several parameters or when you want to use a stored procedure.
As a best practice, it is recommended to use SET FMTONLY ON
and OFF
.
One big advantage of using this option is SqlServer will not generate, compile a query plan, because the actual statement is not being executed.
When SQL Reporting Services and Visual Studio read stored procedures for the first time to retrieve column names, they use SET FMTONLY ON
and OFF
.
When this option is set, only the metadata is read.
Let's see this by example, in your Query Analyzer, make sure you switch on the "Include Actual Execution Plan" to see this in action and run the following queries:
USE Northwind SET FMTONLY ON EXEC dbo.CustOrderHist 'ALFKI' SET FMTONLY OFF SET FMTONLY ON SELECT * FROM dbo.Customers SET FMTONLY OFF SELECT * FROM dbo.Customers where 1=0Only the last statement has the execution plan included.