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

/*,Warehouse technician',*/ insert into @RoleActions (roleid,ActionId,scope) select r.id,g.id,null from roles r , RoleActionTypes g where r.guid='F1C4B065-85CF-46C3-B262-E4FE26D9BA58' and g.guid in (
'E956D023-A3AB-47A2-B3BC-6BFF99337B10')

/*,Technician',*/ insert into @RoleActions (roleid,ActionId,scope) select r.id,g.id,null from roles r , RoleActionTypes g where r.guid='A508E668-94BC-4076-A8EE-972ADAD06E64' and g.guid in (
'E956D023-A3AB-47A2-B3BC-6BFF99337B10')



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



declare @RoleActionRoleScopes table(
	[UserRoleId] [int] NOT NULL,
	[ActionId] [int] NOT NULL,
	[TargetRoleId] [int] NOT NULL,
	[Allowed] [bit] NOT NULL
)


insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='A508E668-94BC-4076-A8EE-972ADAD06E64' and r2.guid='62FD0F19-43D8-4F1E-B7C7-57A8D17A0131' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='F1C4B065-85CF-46C3-B262-E4FE26D9BA58' and r2.guid='62FD0F19-43D8-4F1E-B7C7-57A8D17A0131' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='A508E668-94BC-4076-A8EE-972ADAD06E64' and r2.guid='D0275602-E7DF-420F-9E29-77AF67A7B844' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='F1C4B065-85CF-46C3-B262-E4FE26D9BA58' and r2.guid='D0275602-E7DF-420F-9E29-77AF67A7B844' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='A508E668-94BC-4076-A8EE-972ADAD06E64' and r2.guid='B7B24D8B-3ACE-4B6B-8581-6ABD4375470A' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='F1C4B065-85CF-46C3-B262-E4FE26D9BA58' and r2.guid='B7B24D8B-3ACE-4B6B-8581-6ABD4375470A' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='A508E668-94BC-4076-A8EE-972ADAD06E64' and r2.guid='A6B2BFC3-D2E0-4496-81D0-224F77085DA5' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='F1C4B065-85CF-46C3-B262-E4FE26D9BA58' and r2.guid='A6B2BFC3-D2E0-4496-81D0-224F77085DA5' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='A508E668-94BC-4076-A8EE-972ADAD06E64' and r2.guid='1BF4E983-EC11-4A4C-B02B-8F2F7631DAA9' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='F1C4B065-85CF-46C3-B262-E4FE26D9BA58' and r2.guid='1BF4E983-EC11-4A4C-B02B-8F2F7631DAA9' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='A508E668-94BC-4076-A8EE-972ADAD06E64' and r2.guid='D4F9CE6E-C39F-4179-B2FB-A02F2E4C86B6' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='F1C4B065-85CF-46C3-B262-E4FE26D9BA58' and r2.guid='D4F9CE6E-C39F-4179-B2FB-A02F2E4C86B6' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='A508E668-94BC-4076-A8EE-972ADAD06E64' and r2.guid='14665214-632A-4729-B22B-892CC31B1894' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='F1C4B065-85CF-46C3-B262-E4FE26D9BA58' and r2.guid='14665214-632A-4729-B22B-892CC31B1894' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,0 from roles r1,roles r2, roleactiontypes a where r1.guid='A508E668-94BC-4076-A8EE-972ADAD06E64' and r2.guid='0E9CB844-06B6-4AEA-A0DC-0FC17EE71BE2' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,0 from roles r1,roles r2, roleactiontypes a where r1.guid='F1C4B065-85CF-46C3-B262-E4FE26D9BA58' and r2.guid='0E9CB844-06B6-4AEA-A0DC-0FC17EE71BE2' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='A508E668-94BC-4076-A8EE-972ADAD06E64' and r2.guid='A508E668-94BC-4076-A8EE-972ADAD06E64' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='F1C4B065-85CF-46C3-B262-E4FE26D9BA58' and r2.guid='A508E668-94BC-4076-A8EE-972ADAD06E64' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='A508E668-94BC-4076-A8EE-972ADAD06E64' and r2.guid='F1C4B065-85CF-46C3-B262-E4FE26D9BA58' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')
insert into @RoleActionRoleScopes (userroleid,actionid,targetroleid,allowed) select r1.id,a.id,r2.id,1 from roles r1,roles r2, roleactiontypes a where r1.guid='F1C4B065-85CF-46C3-B262-E4FE26D9BA58' and r2.guid='F1C4B065-85CF-46C3-B262-E4FE26D9BA58' and a.guid in ('E956D023-A3AB-47A2-B3BC-6BFF99337B10','27061901-5A98-4754-90E5-2D46243E5050','BC09021F-6411-45D1-8BB2-752BC8D0495F','0C9D3EA6-DF44-4ACD-A854-06EC8EE6A2E9')


MERGE INTO [dbo].RoleActionRoleScopes AS TARGET
USING @RoleActionRoleScopes AS SOURCE
	ON	TARGET.userroleid = SOURCE.userroleid and
		TARGET.targetroleid = SOURCE.targetroleid and
		TARGET.actionid = SOURCE.actionid
	WHEN MATCHED THEN    
			UPDATE SET allowed=source.allowed
	WHEN NOT MATCHED BY TARGET THEN   
			INSERT ( userroleid,actionid,targetroleid,allowed) VALUES (Source.userroleid,Source.actionid,Source.targetroleid,Source.allowed)  
OUTPUT $action, SOURCE.*;
