編寫sql語句技巧

2021-05-23 08:56:31 字數 1226 閱讀 1689

一、今天遇到乙個帥選資料的問題,sqlserver儲存過程中,加了乙個新的篩選資料的條件,不打亂原來的取資料的模式下把新的功能新增進去,用一段複合型的exists語句就可以解決問題

and 原先的sql 語句

--過濾自助機不顯示的科室、專家資料 add by aorigel 20101020

andnotexists( select 1

from gh_yy_pbzb aa,gh_yy_pbzbmx bb 

where aa.pbzbid = bb.pbzbid and bb.pbmxid = a.pbmxxh

and 1 =

(casea.pbkmlx

--科室

when1 then

casewhen

exists

(select 1 from yy_ksbmk cc

where cc.id = aa.pbmk and isnull(cc.zzghbz,0) =  1

)then1

else0

end

--專家

when0 then

casewhen

exists

(select 1 from yy_zgbmk dd

where dd.id = aa.pbmk and isnull(dd.zzghbz,0) =  1

)then1

else0

end

end

)           

) 解釋:

1、exists 語句的使用

2、case 語句的使用,巢狀使

3、1=1 邏輯使用

SQL語句編寫規範

1.注釋 不換行的簡單注釋,在注釋文字前用 多行的注釋用 2.大小寫規範 sql語句中的所有保留字均需大寫,且不要使用縮寫 例 all,as,case,create,jion,left,right,not,on,out,scelect,table,view等 3.縮排與換行 將sql語句按照子句分行...

sql語句編寫規範

在使用sql查詢時,要提高查詢的效率,我們要盡可能的優化where後的子句,使查詢能走索引,索引查詢的效率更高.索引的優先順序 聚簇索引 非聚簇唯一索引 非聚簇非唯一索引 不走索引 具體包括 1.要使用索引,首先搞清楚,什麼情況下查詢走索引,這樣就能方便我們對己有的sql 優化.2.在where子句...

SQL語句大全 技巧

三 技巧 1 1 1,1 2的使用,在sql語句組合時用的較多 where 1 1 是表示選擇全部 where 1 2 全部不選,如 if strwhere begin set strsql select count as total from tblname where strwhere ende...