---
title: "【SQL】oracle中的多表更新"
author: "duduli"
date: "2019-07-03T14:41:55Z"
tags: []
categories: ["数据库"]
slug: "sql-oracle"
draft: false
lastmod: "2019-07-03T14:41:55Z"
id: 2442359
url: "https://duduli.iteye.com/blog/2442359"
---

对于多表更新标准的sql存在这样的方法

```sql
update t_store_i a,t_accept_d b set a.purtax=b.purtax,a.saletax=b.saletax where a.wareid=b.wareid and a.batid=b.batid and a.compid=7 and a.purtax<>b.purtax and a.saletax<>b.saletax 
```

或者

```sql
update t_store_i a 
set a.purtax=b.purtax,a.saletax=b.saletax 
where exists (select 1
from t_accept_d b
where a.wareid=b.wareid and a.batid=b.batid and a.compid=7 and a.purtax<>b.purtax and a.saletax<>b.saletax
)
```



但是对于oracle来说需要采用merge into的方式才能更新。

```sql

merge into d_20190127 a
using d_20190128 b
on (a.billno = b.billno)
--when matched then update set a.sap_note=b.sap_note    -- 可不执行
when not matched then insert(a.billno,a.transtime,a.sap_status,a.sap_note,a.sap_result) values(b.billno,b.transtime,b.sap_status,b.sap_note,b.sap_result) 
```