

declare @RoleActionTypes table(
	[Name] [nvarchar](50) NOT NULL,
	[guid] [uniqueidentifier] NULL
)

insert into @roleactiontypes (name,guid) values('ManageMfaConfiguration','D395B807-6055-4192-84D1-830CCAB13019')



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



declare @RoleActions table(
	[RoleId] [int] NOT NULL,
	actionid [int] NOT NULL,
	[Scope] [nvarchar](500) NULL
)

/*,Admin',*/ 
insert into @RoleActions (roleid,ActionId,scope) select r.id,g.id,null from roles r , RoleActionTypes g where r.guid='62FD0F19-43D8-4F1E-B7C7-57A8D17A0131' and g.guid in (
'D395B807-6055-4192-84D1-830CCAB13019')

/*,Organization Admin',*/ 
insert into @RoleActions (roleid,ActionId,scope) select r.id,g.id,null from roles r , RoleActionTypes g where r.guid='1BF4E983-EC11-4A4C-B02B-8F2F7631DAA9' and g.guid in (
    'D395B807-6055-4192-84D1-830CCAB13019')


/*,System admin',*/ 
insert into @RoleActions (roleid,ActionId,scope) select r.id,g.id,null from roles r , RoleActionTypes g where r.guid='0E9CB844-06B6-4AEA-A0DC-0FC17EE71BE2' and g.guid in (
    'D395B807-6055-4192-84D1-830CCAB13019')


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