真的不那么难....
你需要:
- 源表(或查询)以提供数据
- 将其合并到的目标表
- 检查这两个表的条件
- 声明如果找到匹配项(在该条件下)该怎么办
- 声明如果找不到(在该条件下)匹配该怎么办
因此,基本上,它类似于:
-- this is your TARGET table - this is where the data goes into MERGE dbo.Sometable AS target -- this is your SOURCE table where the data comes from USING dbo.AnotherTable AS source -- this is the ConDITION they have to "meet" onON (target.SomeColumn = source.AnotherColumn)-- if there's a match, so if that row already exists in the target table,-- then just UPDATE whatever columns in the existing row you want to updateWHEN MATCHED THEN UPDATE SET Name = source.Name, OtherCol = source.SomeCol-- if there's NO match, that is the row in the SOURCE does *NOT* exist in the TARGET yet,-- then typically INSERT the new row with whichever columns you're interested inWHEN NOT MATCHED THEN INSERT (Col1, Col2, ...., ColN) VALUES (source.Val1, source.Val2, ...., source.ValN);



