Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Best practice when getting the structure of the resulting query or table.

, 29 Jun 2011
Rate this:
Please Sign up or sign in to vote.
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=0
Only the last statement has the execution plan included.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

zvin

Canada Canada
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 5 I never knew about this (I'm guilty ... PinprotectorAspDotNetDev29-Jun-11 10:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 29 Jun 2011
Article Copyright 2011 by zvin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid