---
title: "sqlserver 实用的功能 upsert"
author: "onlyor"
date: "2012-04-06T14:45:57Z"
tags: []
categories: ["数据库"]
slug: "sqlserver-upsert"
draft: false
lastmod: "2012-04-06T14:45:57Z"
id: 1477328
url: "https://onlyor.iteye.com/blog/1477328"
---

merge into Target using Source

on (condition)

when matched then 

update set column1=value1

when not matched then

insert (column1,  column2) values (value1, value2);

 

 

解释：

Target : 目标表名

Source: 源表名

Condition： 为匹配条件

当matched时更新，not时则更新