IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_GetSetV002Profile]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_GetSetV002Profile]
/****** Object:  StoredProcedure [dbo].[sp_GetSetV002Profile]    Script Date: 18.11.2015 22:00:23 ******/

--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
--SET ANSI_PADDING ON
--GO

---- =============================================
---- Author:      Vitacore (с)
---- Create date: 2016-06-24
---- Description: Загрузка V002Profile
---- =============================================
--create PROCEDURE [dbo].[sp_GetSetV002Profile]
--    @Xml xml = null,
--    @ID uniqueidentifier = null,
--    @IsNew bit = 1,
--    @QueueName varchar(100) = 'RDSSync',
--    @dateTime datetime = null,
--    @User uniqueidentifier = null
--AS
--BEGIN

--    SET NOCOUNT ON;

--    declare @Result varchar(max)
--           ,@ContentEncoding varchar(255)
--           ,@Type varchar(255)
--           ,@OperationType varchar(255)
--           ,@OwnerID varchar(255)
--           ,@FTLOAD varchar(6)

--    select @FTLOAD = 'FTLoad'

--    IF (@Xml is not null)
--    BEGIN
--         -- Получаем тип операции
--        select @OperationType = t.c.value('./header[1]/TYPE[1]', 'varchar(36)')
--        from @Xml.nodes('V002_PROFILE') T(c)

--		-- Generate table #V002_PROFILE_ITEMS
--        select t.c.value('./ID[1]', 'int')										as [ID]
--		,t.c.value('./NAME[1]', 'varchar(256)')									as [Name]
--		,convert(datetime, t.c.value('./DATE_BEGIN[1]', 'varchar(10)'), 104)	as [DateBegin]
--		,convert(datetime, t.c.value('./DATE_END[1]', 'varchar(10)'), 104)		as [DateEnd]
--        INTO #V002_PROFILE
--        from @Xml.nodes('V002_PROFILE/body/V002_PROFILE_ITEMS/V002_PROFILE_ITEM') T(c)
--        where @OperationType = @FTLOAD

--		if (@OperationType = @FTLOAD)
--        BEGIN

--		update v
--		set v.DateBegin = vp.DateBegin, 
--			v.DateEnd = vp.DateEnd,
--			v.Name = vp.Name
--		from [AKUZ].T_V002_PROFILE v
--		join #V002_PROFILE vp on vp.ID = v.Id


--		INSERT INTO [AKUZ].[T_V002_PROFILE]
--		           ([ID]
--		           ,[NAME]
--		           ,[DateBegin]
--		           ,[DateEnd])
--		     SELECT
--		            v.[ID]
--		           ,v.[NAME]
--		           ,v.[DateBegin]
--		           ,v.[DateEnd]
--				   FROM #V002_PROFILE v
--				   where not exists (select * from [AKUZ].T_V002_PROFILE vp where vp.Id = v.ID)
--		update v
--		set v.DateEnd = getdate()
--		from [AKUZ].T_V002_PROFILE v
--		left join #V002_PROFILE vp on vp.ID = v.Id
--		where vp.Id is null
--        END

--		DROP TABLE #V002_PROFILE
--    END
--END