开发者

What does this query mean?

开发者 https://www.devze.com 2022-12-23 21:05 出处:网络
I\'m reading a book about SQL. In that book, I saw strange query below: SELECT * into mycustomer from customer开发者_JS百科 WHERE 1=2

I'm reading a book about SQL.

In that book, I saw strange query below:

SELECT * into mycustomer from customer开发者_JS百科 WHERE 1=2

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


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.


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.


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消