IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_GetSetDLSBXml]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_GetSetDLSBXml]
--GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
---- =============================================
---- Author:		Vitacore (с)
---- Create date: 2014-02-05
---- Description:	Выгрузка/загрузка связей профилей и отделений в(из) RDS 
---- =============================================
--CREATE PROCEDURE [dbo].[sp_GetSetDLSBXml]
--	@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_DIVISION_LINK_STRUCTURE_BED d  where d.DivisionLinkStructureBedID = @ID))
--		BEGIN
--			select @OperationType = 'Update'
--		END
--		ELSE IF (@IsNew = 0 and not exists (select * from [RDS].T_DIVISION_LINK_STRUCTURE_BED d where d.DivisionLinkStructureBedID = @ID))
--		BEGIN
--			select @OperationType = 'Delete'
--				select @Result = (select 
--				@LpuCode			as 'header/LPU'
--				,@OperationType		as 'header/TYPE'
--				,@ID				as 'header/ID'
--				FOR XML PATH(''),
--				ROOT('DIVISION_LINK_STRUCTURE_BED')
--			) 
--		END
				
--select @ContentEncoding = 'utf-8'
--   ,@Type = 'DIVISION_LINK_STRUCTURE_BED'
--   ,@OwnerID = @ID

--		IF(@OperationType <> 'Delete')
--		BEGIN
--		SELECT @Result = ( 
--		--собираем DIVISION_LINK_STRUCTURE_BED
--			select 
--				@LpuCode			as 'header/LPU'
--				,@OperationType		as 'header/TYPE'
--				,@ID				as 'header/ID'

--				,dlsb.Division as 'body/DIVISION_ID'
--				,dlsb.StructureBed as 'body/STRUCTURE_BED'
--				,dlsb.V020StructureBed as 'body/V020_STRUCTURE_BED'
--			from [RDS].T_DIVISION_LINK_STRUCTURE_BED dlsb
--			where dlsb.DivisionLinkStructureBedID = @ID
--			FOR XML PATH(''),
--			ROOT('DIVISION_LINK_STRUCTURE_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 #DIVISION_LINK_STRUCTURE_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]/DIVISION_ID[1]', 'uniqueidentifier') as [Div]
--				,T.c.value('./body[1]/STRUCTURE_BED[1]', 'int') as [CodeSB]
--				,T.c.value('./body[1]/V020_STRUCTURE_BED[1]', 'int') as [V020StructureBed]
--			INTO #DIVISION_LINK_STRUCTURE_BED
--		from @Xml.nodes('DIVISION_LINK_STRUCTURE_BED') T(c)
--		select @OperationType = (select OperationType from #DIVISION_LINK_STRUCTURE_BED)

--		if(@OperationType <> 'Delete')
--		BEGIN
--		-- INSERT [RDS].T_DIVISION_LINK_STRUCTURE_BED
--		INSERT INTO [RDS].[T_DIVISION_LINK_STRUCTURE_BED]
--		(DivisionLinkStructureBedID ,[Division],[StructureBed],[ID], V020StructureBed)
--		SELECT
--			newID()
--			,(select d.DivisionID from [RDS].T_DIVISION d where (d.ID = dsl.Div or d.DivisionID = dsl.Div))
--			,dsl.CodeSB
--			,dsl.ID
--			,dsl.V020StructureBed
--		from #DIVISION_LINK_STRUCTURE_BED dsl				
--		END
--		ELSE
--		BEGIN
--			delete dsl
--			from [RDS].[T_DIVISION_LINK_STRUCTURE_BED] dsl
--			join #DIVISION_LINK_STRUCTURE_BED b on (b.[ID] = dsl.ID or b.ID = dsl.DivisionLinkStructureBedID)
--		END

--		-- Drop table
--		DROP TABLE #DIVISION_LINK_STRUCTURE_BED

--		END
--END
--GO