代码改变世界

sqlserver MERGE INTO 用法

2016-02-29 17:32  ycm  阅读(3362)  评论(0编辑  收藏  举报
declare @pin varchar(20),@yearStr varchar(20)
set @pin='01082118'
set @yearStr='2017'

MERGE INTO zk_yj_manage AS a USING 
(select @pin as 'pin',@yearStr as 'yearStr' ) AS b
 ON (a.pin= b.pin and a.yearStr = b.yearStr ) 
WHEN MATCHED     THEN  UPDATE   SET basedays=17.00,dtime=getdate()
WHEN NOT MATCHED THEN  INSERT (pin,yearStr) VALUES(@pin,@yearStr);

 在编写存储过程中处理更新插入问题,当存在更新记录,不存在插入记录。