
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 (
'C216EC25-4665-48D3-83CC-ED85FF3AFE95',
'86EAECDA-918A-48EC-B3EC-49D3FEB3F492')


/*,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 (
'C216EC25-4665-48D3-83CC-ED85FF3AFE95',
'86EAECDA-918A-48EC-B3EC-49D3FEB3F492')

    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.*;
