Click here to Skip to main content
11,932,945 members (49,966 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked

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

, 29 Jun 2011 CPOL
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
EXEC dbo.CustOrderHist 'ALFKI'
SELECT * FROM dbo.Customers
SELECT * FROM dbo.Customers where 1=0
Only the last statement has the execution plan included.


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


About the Author

Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 5 I never knew about this (I'm guilty ... Pin
AspDotNetDev29-Jun-11 11:54
protectorAspDotNetDev29-Jun-11 11:54 
Reason for my vote of 5
I never knew about this (I'm guilty of the WHERE 1 = 0 technique).

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 29 Jun 2011
Article Copyright 2011 by zvin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid