tags:

views:

156

answers:

5

I'm reading a book about SQL.

In that book, I saw strange query below:

SELECT * into mycustomer from customer WHERE 1=2

In this query, what is "WHERE 1=2" ?

+2  A: 

1=2 will always be false.

This is a way to specify a WHERE clause that will always evaluate to false.

A similar thing is WHERE 1=1 which always evaluates to true.

codaddict
+1 for adding the 'similar thing' 1=1.
KMan
A: 
WHERE 1=2

Is impossible so that condition will always be false. Is it a typo? Maybe it is to clear a load of variables? As SELECT INTO will put all columns into @COLUMNNAME vars

JD
+14  A: 

Usually used to copy structure of one table to into another, as in your case.

SELECT * INTO mycustomer FROM customer WHERE 1=2

This code creates an identical structure of table Customer in your new table MyCustomer. Note that in SQL Server, the constraints are not copied; so probably you would need to recreate the constraints.

KMan
You are the only person who got it right so far. Good job!
Gabe
I got it! Thanks, KMan!!
zihado
A: 

Its nothing but just like a SQL injecttion in your case it will always false if 1=1 than its true and will return all Customers data

BreakHead
+1  A: 

Back in the old days when I was using classic ASP, I used the "WHERE 1=2" structure to retrieve the column definitions of the table and not its contents. Nowadays there are better ways of retrieving column definitions by using an object-relation mapping framework.

My guess is that the book you are reading is slightly outdated, or the context of this query is misplaced.

Prutswonder