with as 如何與update一起使用


因為with必須緊跟引用的select語句,而不是delete,update,merge等。

所以有2種方式:

方式1.

update table1 b set b.NAME=(
  with t as
  (
    select * from temp
  )
  select a.NAME from table2 a where a.ID=b.ID
  )
where exists(select 1....)

方式2.

和merge into 一起使用

merge into table1 a
using(
  with t as
  (
    select * from ......
  )
  select a.NAME 
  from table2
  inner join t a on ......
) b on (a.id = b.id and ......)
when matched then 
update set 
a.NAME=b.NAME
--delete where a.nums=0

特別說明:
DELETE字句只能寫在MATCHED情況中,不匹配時無法刪除會報錯。
當DELETE跟在UPDATE子句之后時,DELETE字句是針對UPDATE字句修改后的數據進行過濾的。
比如需要刪除所有C字段="0"的數據,UPDATE字句將所有數據的C字段都更新為0,
那么會刪除所有數據,而不是原本為0的數據。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM