1. 程式人生 > >SQL語句 where 1=1 && where 11

SQL語句 where 1=1 && where 11

http://blog.csdn.net/fanyuna/article/details/5972437
where 1=1
最近看到很多sql裡用到where 1=1,原來覺得這沒用嘛,但是又想到如果沒用為什麼要寫呢?於是在網上查了查,在這裡就淺談一下:
1=1 永真, 1<>1 永假。

1<>1 的用處:
用於只取結構不取資料的場合
例如:
create table table_temp tablespace tbs_temp as
select * from table_ori where 1<>1
建成一個與table_ori 結構相同的表table_temp,但是不要table_ori 裡的資料。(除了表結構,其它結構也同理)

1=1的用處
用於動態SQL
例如 lv_string := ‘select tbl_name,tbl_desc from tbl_test where 1=1 ‘||l_condition;
當用戶選擇了查詢的名稱’abc’時l_condition :=’and tbl_name = ”abc””;但是當用戶沒有選擇名稱查詢時,l_condition就為空 這樣 lv_string = ‘select tbl_name,tbl_desc from tbl_test where 1=1 ’ ,執行也不會出錯,相當於沒有限制名稱條件。但是如果沒有1=1的條件,則lv_string =’select tbl_name,tbl_desc from tbl_test where ‘;這樣就會報錯。

除了1=1 或1<>1之外的其它永真永假的條件同理。