IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_GetSetAmountXml]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_GetSetAmountXml]
--GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-02-05
---- Description:Загрузка информации о количестве коек в отделений в RDS 
---- =============================================
--CREATE PROCEDURE [dbo].[sp_GetSetAmountXml]
--	@LpuCode varchar(max) = null,
--	@Xml xml = null,
--	@IsNew bit = 1,
--	@ID uniqueidentifier = null
--AS
--BEGIN

--	declare @Result varchar(max)
--			,@ContentEncoding varchar(255)
--			,@Type varchar(255)
--			,@OperationType varchar(max) = null
--			,@OwnerID uniqueidentifier

--SET NOCOUNT ON;
--	IF (@Xml is null)
--	BEGIN
--	--выясняем тип операции
--		IF(@IsNew = 1)
--		BEGIN
--			select @OperationType = 'Insert'
--		END
--		ELSE IF (@IsNew = 0 and exists (select * from [RDS].T_AMOUNT_BED a  where a.AmountBedID = @ID))
--		BEGIN
--			select @OperationType = 'Update'
--		END
--		ELSE IF (@IsNew = 0 and not exists (select * from [RDS].T_AMOUNT_BED a where a.AmountBedID = @ID))
--		BEGIN
--			select @OperationType = 'Delete'
--				select @Result = (select 
--				@LpuCode				as 'header/LPU'
--				,@OperationType			as 'header/TYPE'
--				,@ID					as 'header/ID'
--				FOR XML PATH(''),
--				ROOT('AMOUNT_BED')
--			) 
--		END
		
--select @ContentEncoding = 'utf-8'
--   ,@Type = 'AMOUNT_BED'
--   ,@OwnerID = @ID

--		IF(@OperationType <> 'Delete')
--		BEGIN
--	SELECT @Result = ( 
--		select 
		
--			@LpuCode			as 'header/LPU'
--			,@OperationType		as 'header/TYPE'
--			,@ID				as 'header/ID'

--			,ab.DivisionLinkStructureBed as 'body/DLSB'
--			,convert(varchar(10), ab.[DateBegin], 20)	as 'body/DATE_BEGIN'
--		    ,convert(varchar(10), ab.[DateEnd], 20)		as 'body/DATE_END'
--		    ,ab.[FemaleBed]		as 'body/FEMALE_BED'
--		    ,ab.[MaleBed]		as 'body/MALE_BED'
--		    ,ab.[BedsCount]		as 'body/BEDS_COUNT'
--		    ,ab.[RepairBed]		as 'body/REPAIR_BED'
--		    ,ab.[PlannedBedsCount]	as 'body/PLANNED_BEDS_COUNT'
--		    ,ab.[PlannedMaleBed]	as 'body/PLANNED_MALE_BED'
--		    ,ab.[PlannedFemaleBed]	as 'body/PLANNED_FEMALE_BED'
--		from [RDS].T_AMOUNT_BED ab
--		where ab.AmountBedID = @ID
--		FOR XML PATH(''),
--		ROOT('AMOUNT_BED')
--	) 
--	END

--		INSERT INTO [RDS].[T_RABBIT_SEND_OBJECT]
--		   ([RabbitSendObjectID]
--		   ,[QueueName]
--		   ,[Content]
--		   ,[MessageID]
--		   ,[Headers]
--		   ,[ContentEncoding]
--		   ,[Type]
--		   ,[UserID]
--		   ,[State]
--		   ,[CreateDateTime]
--		   ,[OwnerID])
--		select
--		   NewID()--(<RabbitSendObjectID, uniqueidentifier,>
--		   ,s.ServerName--,<QueueName, varchar(255),>
--		   ,dbo.ConvertTextToBinaryZip(@Result, '')--,<Content, text,>
--		   ,null--,<MessageID, varchar(255),>
--		   ,null--,<Headers, varchar(255),>
--		   ,@ContentEncoding--,<ContentEncoding, varchar(255),>
--		   ,@Type--,<Type, varchar(255),>
--		   ,null--,<UserID, varchar(255),>
--		   ,0--,<State, int,>)
--		   ,GETDATE()
--		   ,@OwnerID
--		   from [RDS].T_SYNC_SERVERS s
--			where s.ServerType <> 1 and not 
--			exists (select * from [RDS].T_SYNC_SERVERS s1
--			join [RDS].T_SERVERS_LINK sl on sl.SyncServers = s1.SyncServersID
--			join [RDS].T_SERVERS_LINK_LPU ll on ll.ServersLinkID = sl.ServersLinkID
--			join [RDS].T_LPU_MAIN lp on lp.MCOD = ll.LpuMain
--			where lp.MCOD = @LpuCode and s1.SyncServersID = s.SyncServersID)
--			select * from  [RDS].T_RABBIT_SEND_OBJECT
--		END
--	ELSE 
--	IF (@Xml is not null)
--		BEGIN
--		-- Generate table #AMOUNT_BED
--		select				
--				T.c.value('./header[1]/TYPE[1]', 'varchar(max)') as [OperationType]
--				,T.c.value('./header[1]/LPU[1]', 'varchar(6)') as [LpuMain]
--				,T.c.value('./header[1]/ID[1]', 'uniqueidentifier') as [ID]
--				,T.c.value('./body[1]/DLSB[1]', 'uniqueidentifier') as [Dlsb]
--				,T.c.value('./body[1]/DATE_BEGIN[1]', 'datetime') as [DateBegin]
--				,T.c.value('./body[1]/DATE_END[1]', 'datetime') as [DateEnd] 
--				,T.c.value('./body[1]/FEMALE_BED[1]', 'int') as [FemaleBed]
--				,T.c.value('./body[1]/MALE_BED[1]', 'int') as [MaleBed] 
--				,T.c.value('./body[1]/BEDS_COUNT[1]', 'int') as [BedsCount]
--				,T.c.value('./body[1]/REPAIR_BED[1]', 'int') as [RepairBed]
--				,T.c.value('./body[1]/PLANNED_BEDS_COUNT[1]', 'int') as [PlannedBedsCount] 
--				,T.c.value('./body[1]/PLANNED_MALE_BED[1]', 'int') as [PlannedMaleBed]
--				,T.c.value('./body[1]/PLANNED_FEMALE_BED[1]', 'int') as [PlannedFemaleBed]
--			INTO #AMOUNT_BED
--		from @Xml.nodes('AMOUNT_BED') T(c)
--		select @OperationType = (select OperationType from #AMOUNT_BED)

--		if(@OperationType <> 'Delete')
--		BEGIN
--		-- update [RDS].T_AMOUNT_BED
--		update ab
--		set
--			ab.[DateEnd] = am.DateEnd
--			,ab.[FemaleBed] = am.FemaleBed
--			,ab.[MaleBed] = am.MaleBed
--			,ab.[BedsCount] = am.BedsCount
--			,ab.[RepairBed] = am.RepairBed
--			,ab.[PlannedFemaleBed] = ISNULL(am.[PlannedFemaleBed], 0)
--			,ab.[PlannedMaleBed] =  ISNULL(am.[PlannedMaleBed], 0)
--			,ab.[PlannedBedsCount] = ISNULL(am.[PlannedBedsCount], 0)
--			,ab.DivisionLinkStructureBed = (select top 1 dl.DivisionLinkStructureBedID from [RDS].T_DIVISION_LINK_STRUCTURE_BED dl where dl.DivisionLinkStructureBedID = am.Dlsb or dl.ID = am.Dlsb)
--		from [RDS].T_AMOUNT_BED ab
--		join #AMOUNT_BED am on am.ID = ab.ID 
 
--		-- INSERT [RDS].T_AMOUNT_BED
--		INSERT INTO [RDS].[T_AMOUNT_BED]
--		([AmountBedID], [DateBegin], [DateEnd], [DivisionLinkStructureBed], [FemaleBed], [MaleBed], [BedsCount], [RepairBed], [ID],[PlannedFemaleBed], [PlannedMaleBed], [PlannedBedsCount])
--		SELECT
--			newID()
--			,ab.[DateBegin]
--			,ab.[DateEnd]
--			,(select dlsb.[DivisionLinkStructureBedID] from [RDS].T_DIVISION_LINK_STRUCTURE_BED dlsb where (dlsb.ID = ab.Dlsb or dlsb.DivisionLinkStructureBedID = ab.Dlsb))
--			,ab.[FemaleBed]
--			,ab.[MaleBed]
--			,ab.[BedsCount]
--			,ab.[RepairBed]
--			,ab.ID
--			,ISNULL(ab.[PlannedFemaleBed], 0)
--			,ISNULL(ab.[PlannedMaleBed], 0)
--			,ISNULL(ab.[PlannedBedsCount], 0)
--		from #AMOUNT_BED ab
--		where not exists (
--			select * 
--			from [RDS].T_AMOUNT_BED ab1
--			where (ab1.ID = ab.ID or ab.ID = ab1.AmountBedID)
--		)
		
--		END
--		ELSE
--		BEGIN
--			delete ab
--			from [RDS].T_AMOUNT_BED ab1
--			join #AMOUNT_BED ab on (ab1.ID = ab.ID or ab.ID = ab1.AmountBedID)
--		END
--		-- Drop table
--		DROP TABLE #AMOUNT_BED

--		END
--END
--GO