sql判断新增或修改

--add or update schedule admin
declare @schedulecount int
declare @schedulegroupid int
select @schedulegroupid = id from MCM_USER_GROUP where name = 'Schedule Admin';
select @schedulecount = count(*) from MCM_USER where userid='schedule';
if CAST(@schedulecount as int) = CAST(0 as int)
      insert into MCM_USER values('schedule','schedule',1,NULL,NULL,1,'schedule admin',0,NULL,@schedulegroupid);
else
      update MCM_USER set groupid=@schedulegroupid where userid='schedule';

--add or update admin
declare @admincount int
declare @admingroupid int
select @admingroupid= id from MCM_USER_GROUP where name = 'Admin';
select @admincount = count(*) from MCM_USER where userid='admin';
if CAST(@admincount as int) = CAST(0 as int)
     insert into MCM_USER values('admin','admin',1,NULL,NULL,0,'admin',0,NULL,@admingroupid);
else
     update MCM_USER set groupid=@admingroupid where userid='admin';

你可能感兴趣的:(sql)