網站首頁 編程語言 正文
測試環境準備
create table #table1
( id int , name varchar(20) );
go
create table #table2
( id int , name varchar(20) );
go
insert into #table1 ( id, name ) values ( 1, 'a' ), ( 2, null ), ( 3, 'c' ), ( 4, 'd' ), ( 5, 'e' );
insert into #table2 ( id, name ) values ( 1, 'a1' ), ( 2, 'b1' ), ( 3, 'c1' );
1、目標表在from子句中,目標表可以加表別名
----join連接方式(推薦)
update a
set a.name = b.name
from #table1 a inner join #table2 b on b.id = a.id
where a.name is null;
----或子查詢方式
update a
set a.name = ( select b.name from #table2 b where a.id = b.id )
from #table1 a
where a.name is null;
2、目標表不在from子句中,目標表不能加表別名
---update … from(推薦)
update #table1
set #table1.name = b.name
from #table2 b
where #table1.id = b.id and #table1.name is null;
--或子查詢方式
update #table1
set name = ( select b.name from #table2 b where #table1.id = b.id )
where name is null;
3、merge更新
merge #table1 a --要更新的目標表
using #table2 b --源表
on a.id = b.id and a.name is null--更新條件(即主鍵)
when matched --如果匹配,將源表指定列的值更新到目標表中
then update set a.name = b.name
when not matched
then insert values ( id, name ); --如果兩個條件都不匹配,將源表指定列的值插入到目標表中。此語句必須以分號結束
清除測試數據
select * from #table1;
select * from #table2;
drop table #table1;
drop table #table2;
原文鏈接:https://www.cnblogs.com/springsnow/p/9592758.html
相關推薦
- 2022-10-12 C#設計模式之建造者模式生成器模式示例詳解_C#教程
- 2023-07-14 echarts圖表進度條類型圖
- 2022-06-10 FreeRTOS實時操作系統之可視化追蹤調試_操作系統
- 2022-04-27 ABP框架中的事件總線功能介紹_實用技巧
- 2022-11-23 使用Xshell建立連接并操縱服務器的方法_Linux
- 2022-05-03 ASP.NET?Core基于滑動窗口實現限流控制_實用技巧
- 2022-06-21 Android?Studio實現簡易登錄界面制作_Android
- 2022-03-27 ASP.NET?HttpRequest類介紹_基礎應用
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支