declare @MFADefaultSendingMethodId int, @MFAEnabledId int

select @MFAEnabledId = id from usersettingstypes where rowid='233083B7-1905-4A1F-8DA2-975EA35A60FC' --MFAEnabled
select @MFADefaultSendingMethodId = id from usersettingstypes where rowid='5031F15D-0DC3-43A5-84CA-6D0723BD815A' --MFADefaultSendingMethod

declare @UserSettings table(
	cmsuserid [int] NOT NULL,
	settingtypeid [int] NOT NULL,
	[value] [nvarchar](1024) NULL
)

insert into @UserSettings
    select m.cmsuserid,@MFAEnabledId,'True' from cmsusertbl m left outer join usersettings s on m.cmsuserid=s.cmsuserid and settingtypeid=@MFAEnabledId where m.securitylevelid<3
union all
    select m.cmsuserid,@MFADefaultSendingMethodId,'Sms' from cmsusertbl m left outer join usersettings s on m.cmsuserid=s.cmsuserid and settingtypeid=@MFADefaultSendingMethodId
    where s.id is null and m.securitylevelid<3
order by 1,2


    MERGE INTO [dbo].UserSettings AS TARGET
    USING @UserSettings AS SOURCE
ON TARGET.cmsuserid = SOURCE.cmsuserid
    and TARGET.settingtypeid= SOURCE.settingtypeid
    WHEN MATCHED THEN
UPDATE SET [value]=source.[value]
    WHEN NOT MATCHED BY TARGET THEN
INSERT ( cmsuserid,settingtypeid,[value]) VALUES (Source.cmsuserid,Source.settingtypeid,Source.[value])
    OUTPUT $action, SOURCE.*;
