栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在SQL Server 2012中使用MERGE插入/更新数据

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在SQL Server 2012中使用MERGE插入/更新数据

真的不那么难....

你需要:

  • 源表(或查询)以提供数据
  • 将其合并到的目标表
  • 检查这两个表的条​​件
  • 声明如果找到匹配项(在该条件下)该怎么办
  • 声明如果找不到(在该条件下)匹配该怎么办

因此,基本上,它类似于:

-- 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);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392600.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号