IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_RDS_UpdateHospReferralXml]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_RDS_UpdateHospReferralXml]
GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-03-01
---- Description:	Обновление даты госпитализации по направлению 
---- =============================================
--CREATE PROCEDURE [dbo].[sp_RDS_UpdateHospReferralXml]
--	@LpuCode varchar(max) = null,
--	@Xml xml = null,
--	@IsNew bit = 1,
--	@ID uniqueidentifier = null,
--	@LpuTo varchar(max) = null,
--	@QueueName varchar(255) = null
--AS
--BEGIN
--SET NOCOUNT ON;
--	declare @Result varchar(max)
--			,@ContentEncoding varchar(255)
--			,@Type varchar(255)
--			,@OperationType varchar(255)
--			,@OwnerID uniqueidentifier

--set @OperationType = 'Update'

--select @ContentEncoding = 'utf-8'
--   ,@Type = 'HOSPITALISATION_REFERRAL'
--   ,@OwnerID = @ID

--	SELECT @Result = ( 
--		select 
--			@LpuCode			as 'header/LPU'
--		,	@OperationType		as 'header/TYPE'
--		,	@ID				as 'header/ID'
--		,	@LpuTo				as 'header/LPU_TO'
--		,	(
--				select
--					lp.LpuCode											as 'REFERRAL_LPU'
--				,	rhr.ReferralNumber									as 'REFERRAL_NUMBER'
--				,	convert(varchar(10), rhr.ReferralDate, 20)			as 'REFERRAL_DATE'
--				,	convert(varchar(10), rhr.HospitalisationDate, 20)	as 'HOSPITALISATION_DATE'
--				from [AKUZ].T_RDS_HOSPITALIZATION_REFERRAL rhr 
--				join [AKUZ].T_BRANCH br on br.LpuID = rhr.BranchFrom		
--				join [AKUZ].T_LPU_MAIN lp on lp.LpuID = br.LpuMain
--				where rhr.ID = @ID
--				FOR XML PATH(''), ROOT('body'), TYPE
--			)
--		FOR XML PATH(''), ROOT('HOSPITALISATION_REFERRAL')
--	);

--	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
--GO
