where 1=1最近看到很多sql里用到where 1=1,原來覺得這沒用嘛,但是又想到如果沒用為什么要寫呢?于是在網上查了查,在這里就淺談一下:1=1 永真, 1<>1 永假。1<>1 的用處:用于只取結構不取數據的場合例如:create table table_temp tablespace tbs_temp asselect * 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之外的其它永真永假的條件同理。
posted on 2012-07-12 22:29
zhanghu198901 閱讀(966)
評論(0) 編輯 收藏