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

--	declare @Result varchar(max)
--			,@ContentEncoding varchar(255)
--			,@Type varchar(255)
--			,@OperationType varchar(255)
--			,@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 [AKUZ].T_FREE_BEDS_INFORMATION fb  where fb.FreeBedsInformationID = @ID))
--		BEGIN
--			select @OperationType = 'Update'
--		END
--		ELSE IF (@IsNew = 0 and not exists (select * from [AKUZ].T_FREE_BEDS_INFORMATION fb where fb.FreeBedsInformationID = @ID))
--		BEGIN
--			select @OperationType = 'Delete'
--				select @Result = (select 
--				@LpuCode				as 'header/LPU'
--				,@OperationType			as 'header/TYPE'
--				,@ID					as 'header/ID'
--				FOR XML PATH(''),
--				ROOT('FREE_BEDS_INFORMATION')
--			) 
--		END
		
--	select @ContentEncoding = 'utf-8'
--   ,@Type = 'FREE_BEDS_INFORMATION'
--   ,@OwnerID = @ID
--		IF(@OperationType <> 'Delete')
--		BEGIN
--	SELECT @Result = ( 
--		select 
		
--			@LpuCode									as 'header/LPU'
--			,@OperationType								as 'header/TYPE'
--			,@ID										as 'header/ID'
--			,fb.DivisionLinkStructureBed				as 'body/DLSB'
--			,convert(varchar(10), fb.[ActualDate], 20)	as 'body/ACTUAL_DATE'
--		    ,fb.[Amount]								as 'body/AMOUNT'
--		from [AKUZ].T_FREE_BEDS_INFORMATION fb
--		where fb.FreeBedsInformationID = @ID
--		FOR XML PATH(''),
--		ROOT('FREE_BEDS_INFORMATION')
--	) 
--	END

--		INSERT INTO [AKUZ].[T_RABBIT_SEND_OBJECT]
--		   ([RabbitSendObjectID]
--		   ,[QueueName]
--		   ,[Content]
--		   ,[MessageID]
--		   ,[Headers]
--		   ,[ContentEncoding]
--		   ,[Type]
--		   ,[UserID]
--		   ,[State]
--		   ,[CreateDateTime]
--		   ,[OwnerID])
--		select
--		   NewID()--(<RabbitSendObjectID, uniqueidentifier,>
--		   ,@QueueName--,<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
--		END
--END
--GO