Justin Standard
Justin Standard

Reputation: 21545

What is an efficient way to exclude all results from a sql query?

Surprised I haven't seen this before, but googling doesn't yield much insight.

I'm working with a framework that is going to generate a SQL query, like it or not. I have the ability to add filtering on the query, and a case has arisen where I wish to exclude all possible results.

My initial thought is to add WHERE 1 = 0 to the WHERE clause of the query.

Is there a more optimized or efficient way to do this?

Since this answer could vary between platforms, this will be run on SQL Server 2008

Upvotes: 2

Views: 99

Answers (2)

Bacon Bits
Bacon Bits

Reputation: 32180

WHERE 1 = 0 is the most common method. Creating temp table structures with SELECT ... INTO #temp WHERE 1 = 0 is pretty common.

Be advised that this condition can cause the query engine to short circuit the query planning, so be careful if you use it for query analysis.

Upvotes: 1

KM.
KM.

Reputation: 103637

you are correct, this is the best and most frequently used way to exclude all results from a query:

WHERE 1=0

Upvotes: 2

Related Questions