sql如果存在就修改不存在就新增

IF EXISTS (SELECT 1 FROM 表名 WHERE 条件)
UPDATE 表名 SET 字段=WHERE 条件
ELSE
INSERT INTO 表名(字段) VALUES(值)

真实使用举例:

if exists(select 1 from [UserRules] where UserID=@UserID)
update [UserRules] set [AttendanceRulesID]=@AttendanceRulesID where UserID=@UserID
else
insert into [UserRules] (UserID,AttendanceRulesID) Values(@UserID,@AttendanceRulesID)

 

posted @ 2015-10-20 10:27  wuyubing  阅读(13755)  评论(0编辑  收藏  举报