

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

insert into @roleactiontypes (name,guid) values('AllowUnattached','DAB18412-7FBE-4E2B-973D-8530D1F52A1A')



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
)

/*,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 (
'DAB18412-7FBE-4E2B-973D-8530D1F52A1A')

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