Sql service 中的“if ... else...” 語句

一.格式
if exists(條件判斷) {執行語句1} else {執行語句2}
二.實例

--現有數據表:【Table1】
--查詢語句: Select1:   select cols1 from Table1 where cols1=0
--更新語句: Update1: update Table1  set cols1=1 where cols1=0
--添加語句: Insert1:    insert into Table1  cols1 values(0)
if exists(Select1)  Update1  else  Insert1
--如果 Table1 表中存在字段值 cols1 =0 則將cols1 的值改爲1,否則添加數據 cols1 =0
--exists()作爲條件判斷,當 Select1 查詢結果不爲空,則返回true,執行 if 下屬的語句,如果 Select1 結果爲空,返回false,執行 else的下屬語句。
--not exists() 反之。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章