﻿ALTER PROCEDURE [dbo].[sp_GroupsUpdateInsert]
			@SerialNumber		nvarchar(255) = null
		   ,@Picture			NVARCHAR(255) = null
		   ,@DisplayName		nvarchar(255)
		   ,@Description		nvarchar(255)
           ,@Embedded			bit
           ,@LastModified		datetime
           ,@LastModifiedBy		nvarchar(50)
		   ,@StaticGroupId		int = NULL
		   ,@ExtraProperties	nvarchar(max)
		   ,@new_id				int				output 
		   ,@errorNumber		int				output
		   ,@errorMessage		nvarchar(250)	output
AS
BEGIN

	set @errorNumber = 0; 	set @errorMessage = space(0);
	
	declare @GroupId int
	set @GroupId = dbo.foo_IdGetGroup(@SerialNumber)

	--declare @TemplateId int
	--set @TemplateId = dbo.foo_IdGetTemplate(@Template)
	
	if(exists(select Id from [dbo].[Groups] where Id = @GroupId))
	begin
		set @new_id = @GroupId;
		exec dbo.sp_GroupsUpdate		 @GroupId
									,@SerialNumber
									,@Picture
									,@DisplayName	
									,@Description	
									,@Embedded
									,@LastModified		
									,@LastModifiedBy	
									,@StaticGroupId
									,@ExtraProperties	
									,@errorNumber output, @errorMessage output
	end
	else
	begin
	
        exec dbo.sp_GroupsInsert		 @SerialNumber
									,@Picture		
									,@DisplayName	
									,@Embedded
									,@Description	
									,@LastModified		
									,@LastModifiedBy
									,@StaticGroupId
									,@ExtraProperties		
									,@errorNumber output, @errorMessage output, @new_id output
	end
END