﻿-----------------------------------------------------
--[dbo].[usp_Import_80_Folder_Host_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Folder_Host_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Folder_Host_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Folder_Host_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [host_id] uniqueidentifier,
       [folder_id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [host_id], [folder_id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [host_id] uniqueidentifier 'host_id',
       [folder_id] uniqueidentifier 'folder_id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Folder_Host]
    SET
        [80.Folder_Host].[host_id] = src.[host_id],
        [80.Folder_Host].[folder_id] = src.[folder_id],
        [80.Folder_Host].[usn] = src.[usn]
    FROM [80.Folder_Host] INNER JOIN @imported_changes src
    ON [80.Folder_Host].id = src.id    AND [80.Folder_Host].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.Folder_Host]([id], [host_id], [folder_id], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[host_id],
        src.[folder_id],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Folder_Host] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Folder_Host_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Folder_Host_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Folder_Host_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Folder_Host_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Folder_Host]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Folder_Host].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_ObjectsInJobs_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_ObjectsInJobs_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_ObjectsInJobs_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_ObjectsInJobs_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [type] int,
       [location] nvarchar(1024),
       [approx_size] bigint,
       [folder_id] uniqueidentifier,
       [id] uniqueidentifier,
       [job_id] uniqueidentifier,
       [object_id] uniqueidentifier,
       [vss_options] xml,
       [order_no] int,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([type], [location], [approx_size], [folder_id], [id], [job_id], [object_id], [vss_options], [order_no], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [type] int 'type',
       [location] nvarchar(1024) 'location',
       [approx_size] bigint 'approx_size',
       [folder_id] uniqueidentifier 'folder_id',
       [id] uniqueidentifier 'id',
       [job_id] uniqueidentifier 'job_id',
       [object_id] uniqueidentifier 'object_id',
       [vss_options] xml 'vss_options/*',
       [order_no] int 'order_no',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.ObjectsInJobs]
    SET
        [80.ObjectsInJobs].[type] = src.[type],
        [80.ObjectsInJobs].[location] = src.[location],
        [80.ObjectsInJobs].[approx_size] = src.[approx_size],
        [80.ObjectsInJobs].[folder_id] = src.[folder_id],
        [80.ObjectsInJobs].[job_id] = src.[job_id],
        [80.ObjectsInJobs].[object_id] = src.[object_id],
        [80.ObjectsInJobs].[vss_options] = src.[vss_options],
        [80.ObjectsInJobs].[order_no] = src.[order_no],
        [80.ObjectsInJobs].[usn] = src.[usn]
    FROM [80.ObjectsInJobs] INNER JOIN @imported_changes src
    ON [80.ObjectsInJobs].id = src.id    AND [80.ObjectsInJobs].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.ObjectsInJobs]([type], [location], [approx_size], [folder_id], [id], [job_id], [object_id], [vss_options], [order_no], [usn], db_instance_id)
    SELECT
        src.[type],
        src.[location],
        src.[approx_size],
        src.[folder_id],
        src.[id],
        src.[job_id],
        src.[object_id],
        src.[vss_options],
        src.[order_no],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.ObjectsInJobs] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_ObjectsInJobs_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_ObjectsInJobs_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_ObjectsInJobs_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_ObjectsInJobs_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.ObjectsInJobs]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.ObjectsInJobs].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Soap_creds_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Soap_creds_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Soap_creds_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Soap_creds_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [port] int,
       [enabled] bit,
       [useproxy] bit,
       [savepassword] bit,
       [proxyip] nvarchar(1024),
       [host_id] uniqueidentifier,
       [proxyport] int,
       [id] uniqueidentifier,
       [creds] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([port], [enabled], [useproxy], [savepassword], [proxyip], [host_id], [proxyport], [id], [creds], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [port] int 'port',
       [enabled] bit 'enabled',
       [useproxy] bit 'useproxy',
       [savepassword] bit 'savepassword',
       [proxyip] nvarchar(1024) 'proxyip',
       [host_id] uniqueidentifier 'host_id',
       [proxyport] int 'proxyport',
       [id] uniqueidentifier 'id',
       [creds] uniqueidentifier 'creds',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Soap_creds]
    SET
        [80.Soap_creds].[port] = src.[port],
        [80.Soap_creds].[enabled] = src.[enabled],
        [80.Soap_creds].[useproxy] = src.[useproxy],
        [80.Soap_creds].[savepassword] = src.[savepassword],
        [80.Soap_creds].[proxyip] = src.[proxyip],
        [80.Soap_creds].[host_id] = src.[host_id],
        [80.Soap_creds].[proxyport] = src.[proxyport],
        [80.Soap_creds].[creds] = src.[creds],
        [80.Soap_creds].[usn] = src.[usn]
    FROM [80.Soap_creds] INNER JOIN @imported_changes src
    ON [80.Soap_creds].id = src.id    AND [80.Soap_creds].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.Soap_creds]([port], [enabled], [useproxy], [savepassword], [proxyip], [host_id], [proxyport], [id], [creds], [usn], db_instance_id)
    SELECT
        src.[port],
        src.[enabled],
        src.[useproxy],
        src.[savepassword],
        src.[proxyip],
        src.[host_id],
        src.[proxyport],
        src.[id],
        src.[creds],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Soap_creds] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Soap_creds_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Soap_creds_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Soap_creds_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Soap_creds_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Soap_creds]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Soap_creds].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_BJobs_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_BJobs_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_BJobs_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_BJobs_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [latest_result] int,
       [target_type] int,
       [id] uniqueidentifier,
       [is_deleted] bit,
       [options] xml,
       [platform] int,
       [target_host_id] uniqueidentifier,
       [schedule] xml,
       [vss_options] xml,
       [target_file] nvarchar(2000),
       [vcb_host_id] uniqueidentifier,
       [description] nvarchar(1024),
       [name] nvarchar(255),
       [schedule_enabled] bit,
       [type] int,
       [target_dir] nvarchar(2000),
       [parent_schedule_id] uniqueidentifier,
       [repository_id] uniqueidentifier,
       [pwd_key_id] uniqueidentifier,
	   [usn] bigint,
       [next_run_time] datetime
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([latest_result], [target_type], [id], [is_deleted], [options], [platform], [target_host_id], [schedule], [vss_options], [target_file], [vcb_host_id], [description], [name], [schedule_enabled], [type], [target_dir], [parent_schedule_id], [repository_id], [pwd_key_id], [usn], [next_run_time])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [latest_result] int 'latest_result',
       [target_type] int 'target_type',
       [id] uniqueidentifier 'id',
       [is_deleted] bit 'is_deleted',
       [options] xml 'options/*',
       [platform] int 'platform',
       [target_host_id] uniqueidentifier 'target_host_id',
       [schedule] xml 'schedule/*',
       [vss_options] xml 'vss_options/*',
       [target_file] nvarchar(2000) 'target_file',
       [vcb_host_id] uniqueidentifier 'vcb_host_id',
       [description] nvarchar(1024) 'description',
       [name] nvarchar(255) 'name',
       [schedule_enabled] bit 'schedule_enabled',
       [type] int 'type',
       [target_dir] nvarchar(2000) 'target_dir',
       [parent_schedule_id] uniqueidentifier 'parent_schedule_id',
       [repository_id] uniqueidentifier 'repository_id',
       [pwd_key_id] uniqueidentifier 'pwd_key_id',
	   [usn] bigint 'usn',
       [next_run_time] datetime 'next_run_time'
    ) WHERE [type] < 100 OR [type] = 202; /* Do not import util jobs */
    EXEC sp_xml_removedocument @idoc

	MERGE [80.BJobs] tgt
	USING @imported_changes src ON tgt.id = src.id AND tgt.db_instance_id = @dbInstanceId
	WHEN MATCHED THEN
		UPDATE SET
			tgt.[latest_result] = src.[latest_result],
			tgt.[target_type] = src.[target_type],
			tgt.[is_deleted] = src.[is_deleted],
			tgt.[options] = src.[options],
			tgt.[platform] = src.[platform],
			tgt.[target_host_id] = src.[target_host_id],
			tgt.[schedule] = src.[schedule],
			tgt.[vss_options] = src.[vss_options],
			tgt.[target_file] = src.[target_file],
			tgt.[vcb_host_id] = src.[vcb_host_id],
			tgt.[description] = src.[description],
			tgt.[name] = src.[name],
			tgt.[schedule_enabled] = src.[schedule_enabled],
			tgt.[type] = src.[type],
			tgt.[target_dir] = src.[target_dir],
			tgt.[parent_schedule_id] = src.[parent_schedule_id],
			tgt.[repository_id] = src.[repository_id],
			tgt.[pwd_key_id] = src.[pwd_key_id],
			tgt.[usn] = src.[usn],
			tgt.[next_run_time] = DATEADD(minute, -@TimeZoneShift, src.[next_run_time])
	WHEN NOT MATCHED BY TARGET THEN
		INSERT ([latest_result], [target_type], [id], [is_deleted], [options], [platform], [target_host_id], [schedule], [vss_options], [target_file], [vcb_host_id], [description], [name], [schedule_enabled], [type], [target_dir], [parent_schedule_id], [repository_id], [pwd_key_id], [usn], [next_run_time], db_instance_id)
		VALUES (
			src.[latest_result],
			src.[target_type],
			src.[id],
			src.[is_deleted],
			src.[options],
			src.[platform],
			src.[target_host_id],
			src.[schedule],
			src.[vss_options],
			src.[target_file],
			src.[vcb_host_id],
			src.[description],
			src.[name],
			src.[schedule_enabled],
			src.[type],
			src.[target_dir],
			src.[parent_schedule_id],
			src.[repository_id],
			src.[pwd_key_id],
			src.[usn],
			DATEADD(minute, -@TimeZoneShift, src.[next_run_time]),
			@dbInstanceId
		)
	OUTPUT
		inserted.[id] AS [id],
		CASE $action WHEN 'INSERT' THEN 0 ELSE 1 END AS [change_type];
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_BJobs_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_BJobs_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_BJobs_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_BJobs_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.BJobs]
	OUTPUT deleted.[id]S
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.BJobs].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_BObjects_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_BObjects_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_BObjects_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_BObjects_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [object_name] nvarchar(2000),
       [guest_os] xml,
       [host_id] uniqueidentifier,
       [object_id] nvarchar(434),
       [viobject_type] nvarchar(50),
       [type] int,
       [unique_key_hash] varbinary(max),
       [display_name] nvarchar(256),
       [platform] int,
	   [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [object_name], [guest_os], [host_id], [object_id], [viobject_type], [type], [unique_key_hash], [display_name], [platform], [usn])
	SELECT   
		T.id,
		T.object_name,
		T.guest_os,
		T.host_id,
		T.object_id,
		T.viobject_type,
		T.type,
		T.unique_key_hash.value('.', 'varbinary(max)'),
		T.display_name,
		T.platform,
		T.usn
	FROM OPENXML( @idoc, '/Root/row', 2)
	WITH
	(
		[id] uniqueidentifier 'id',
		[object_name] nvarchar(2000) 'object_name',
		[guest_os] xml 'guest_os/*',
		[host_id] uniqueidentifier 'host_id',
		[object_id] nvarchar(434) 'object_id',
		[viobject_type] nvarchar(50) 'viobject_type',
		[type] int 'type',
		[unique_key_hash] xml 'unique_key_hash',
		[display_name] nvarchar(256) 'display_name',
		[platform] int 'platform',
		[usn] bigint 'usn'
	)  as T
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.BObjects]
    SET
        [80.BObjects].[object_name] = src.[object_name],
        [80.BObjects].[guest_os] = src.[guest_os],
        [80.BObjects].[host_id] = src.[host_id],
        [80.BObjects].[object_id] = src.[object_id],
        [80.BObjects].[viobject_type] = src.[viobject_type],
        [80.BObjects].[type] = src.[type],
        [80.BObjects].[unique_key_hash] = src.[unique_key_hash],
        [80.BObjects].[display_name] = src.[display_name],
        [80.BObjects].[platform] = src.[platform],
        [80.BObjects].[usn] = src.[usn]
    FROM [80.BObjects] INNER JOIN @imported_changes src
    ON [80.BObjects].id = src.id    AND [80.BObjects].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.BObjects]([id], [object_name], [guest_os], [host_id], [object_id], [viobject_type], [type], [unique_key_hash], [display_name], [platform], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[object_name],
        src.[guest_os],
        src.[host_id],
        src.[object_id],
        src.[viobject_type],
        src.[type],
        src.[unique_key_hash],
        src.[display_name],
        src.[platform],
		src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.BObjects] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_BObjects_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_BObjects_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_BObjects_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_BObjects_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.BObjects]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.BObjects].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_Backups_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_Backups_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_Backups_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_Backups_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [job_source_type] int,
       [id] uniqueidentifier,
       [job_name] nvarchar(255),
       [job_target_host_protocol] int,
       [job_target_host_id] uniqueidentifier,
       [job_target_type] int,
       [job_id] uniqueidentifier,
       [repository_id] uniqueidentifier,
       [platform] int,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([job_source_type], [id], [job_name], [job_target_host_protocol], [job_target_host_id], [job_target_type], [job_id], [repository_id], [platform], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [job_source_type] int 'job_source_type',
       [id] uniqueidentifier 'id',
       [job_name] nvarchar(255) 'job_name',
       [job_target_host_protocol] int 'job_target_host_protocol',
       [job_target_host_id] uniqueidentifier 'job_target_host_id',
       [job_target_type] int 'job_target_type',
       [job_id] uniqueidentifier 'job_id',
       [repository_id] uniqueidentifier 'repository_id',
       [platform] int 'platform',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Backup.Model.Backups]
    SET
        [80.Backup.Model.Backups].[job_source_type] = src.[job_source_type],
        [80.Backup.Model.Backups].[job_name] = src.[job_name],
        [80.Backup.Model.Backups].[job_target_host_protocol] = src.[job_target_host_protocol],
        [80.Backup.Model.Backups].[job_target_host_id] = src.[job_target_host_id],
        [80.Backup.Model.Backups].[job_target_type] = src.[job_target_type],
        [80.Backup.Model.Backups].[job_id] = src.[job_id],
        [80.Backup.Model.Backups].[repository_id] = src.[repository_id],
        [80.Backup.Model.Backups].[platform] = src.[platform],
        [80.Backup.Model.Backups].[usn] = src.[usn]
    FROM [80.Backup.Model.Backups] INNER JOIN @imported_changes src
    ON [80.Backup.Model.Backups].id = src.id    AND [80.Backup.Model.Backups].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.Backup.Model.Backups]([job_source_type], [id], [job_name], [job_target_host_protocol], [job_target_host_id], [job_target_type], [job_id], [repository_id], [platform], [usn], db_instance_id)
    SELECT
        src.[job_source_type],
        src.[id],
        src.[job_name],
        src.[job_target_host_protocol],
        src.[job_target_host_id],
        src.[job_target_type],
        src.[job_id],
        src.[repository_id],
        src.[platform],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Backup.Model.Backups] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_Backups_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_Backups_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_Backups_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_Backups_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Backup.Model.Backups]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Backup.Model.Backups].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_Storages_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_Storages_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_Storages_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_Storages_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [block_size] int,
       [backup_id] uniqueidentifier,
       [stats] xml,
       [modification_time] datetime,
       [version] int,
       [id] uniqueidentifier,
       [file_path] nvarchar(1000),
       [creation_time] datetime,
       [host_id] uniqueidentifier,
       [full_size] bigint,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([block_size], [backup_id], [stats], [modification_time], [version], [id], [file_path], [creation_time], [host_id], [full_size], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [block_size] int 'block_size',
       [backup_id] uniqueidentifier 'backup_id',
       [stats] xml 'stats/*',
       [modification_time] datetime 'modification_time',
       [version] int 'version',
       [id] uniqueidentifier 'id',
       [file_path] nvarchar(1000) 'file_path',
       [creation_time] datetime 'creation_time',
       [host_id] uniqueidentifier 'host_id',
       [full_size] bigint 'full_size',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc

		DECLARE @cur_backup_id uniqueidentifier
		SET @cur_backup_id = (SELECT TOP(1) id FROM @imported_changes WHERE full_size IS NULL)
		WHILE @cur_backup_id IS NOT NULL
		BEGIN
			DECLARE @xml XML
			SET @xml = (SELECT TOP(1) stats FROM @imported_changes WHERE id = @cur_backup_id)

			IF @xml IS NULL
				UPDATE @imported_changes
				SET full_size = 0
				WHERE id = @cur_backup_id
			ELSE
				BEGIN
					EXEC sp_xml_preparedocument @idoc OUTPUT, @xml

					UPDATE @imported_changes
					SET full_size = 
						(SELECT
							CASE
								WHEN size IS NULL THEN 0
								ELSE size
							END
						FROM OPENXML( @idoc, 'stats/CBackupStats/BackupSize', 3)
						WITH (size bigint 'text()'))
					WHERE id = @cur_backup_id
				
					UPDATE @imported_changes
					SET full_size = 0
					WHERE id = @cur_backup_id AND full_size IS NULL

					EXEC sp_xml_removedocument @idoc
				END
		
			SET @cur_backup_id = (SELECT TOP(1) id FROM @imported_changes WHERE full_size IS NULL)
		END


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    UPDATE @imported_changes 
	SET
		[creation_time] = DATEADD( minute, -@TimeZoneShift, [creation_time]),
		[modification_time] = DATEADD( minute, -@TimeZoneShift, [modification_time])
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Backup.Model.Storages]
    SET
        [80.Backup.Model.Storages].[block_size] = src.[block_size],
        [80.Backup.Model.Storages].[backup_id] = src.[backup_id],
        [80.Backup.Model.Storages].[stats] = src.[stats],
        [80.Backup.Model.Storages].[modification_time] = src.[modification_time],
        [80.Backup.Model.Storages].[version] = src.[version],
        [80.Backup.Model.Storages].[file_path] = src.[file_path],
        [80.Backup.Model.Storages].[creation_time] = src.[creation_time],
        [80.Backup.Model.Storages].[host_id] = src.[host_id],
        [80.Backup.Model.Storages].[full_size] = src.[full_size],
        [80.Backup.Model.Storages].[usn] = src.[usn]
    FROM [80.Backup.Model.Storages] INNER JOIN @imported_changes src
    ON [80.Backup.Model.Storages].id = src.id    AND [80.Backup.Model.Storages].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.Backup.Model.Storages]([block_size], [backup_id], [stats], [modification_time], [version], [id], [file_path], [creation_time], [host_id], [full_size], [usn], db_instance_id)
    SELECT
        src.[block_size],
        src.[backup_id],
        src.[stats],
        src.[modification_time],
        src.[version],
        src.[id],
        src.[file_path],
        src.[creation_time],
        src.[host_id],
        src.[full_size],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Backup.Model.Storages] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_Storages_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_Storages_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_Storages_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_Storages_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Backup.Model.Storages]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Backup.Model.Storages].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_HostsByJobs_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_HostsByJobs_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_HostsByJobs_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_HostsByJobs_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [job_id] uniqueidentifier,
       [host_id] uniqueidentifier,
       [id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([job_id], [host_id], [id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [job_id] uniqueidentifier 'job_id',
       [host_id] uniqueidentifier 'host_id',
       [id] uniqueidentifier 'id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.HostsByJobs]
    SET
        [80.HostsByJobs].[job_id] = src.[job_id],
        [80.HostsByJobs].[host_id] = src.[host_id],
        [80.HostsByJobs].[usn] = src.[usn]
    FROM [80.HostsByJobs] INNER JOIN @imported_changes src
    ON [80.HostsByJobs].id = src.id    AND [80.HostsByJobs].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.HostsByJobs]([job_id], [host_id], [id], [usn], db_instance_id)
    SELECT
        src.[job_id],
        src.[host_id],
        src.[id],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.HostsByJobs] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_HostsByJobs_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_HostsByJobs_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_HostsByJobs_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_HostsByJobs_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.HostsByJobs]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.HostsByJobs].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_Points_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_Points_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_Points_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_Points_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [link_id] uniqueidentifier,
       [num] numeric,
       [id] uniqueidentifier,
       [alg] int,
       [backup_id] uniqueidentifier,
       [group_id] uniqueidentifier,
       [type] int,
       [creation_time] datetime,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([link_id], [num], [id], [alg], [backup_id], [group_id], [type], [creation_time], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [link_id] uniqueidentifier 'link_id',
       [num] numeric 'num',
       [id] uniqueidentifier 'id',
       [alg] int 'alg',
       [backup_id] uniqueidentifier 'backup_id',
       [group_id] uniqueidentifier 'group_id',
       [type] int 'type',
       [creation_time] datetime 'creation_time',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    UPDATE @imported_changes 
	SET[creation_time] = DATEADD( minute, -@TimeZoneShift, [creation_time])
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Backup.Model.Points]
    SET
        [80.Backup.Model.Points].[link_id] = src.[link_id],
        [80.Backup.Model.Points].[num] = src.[num],
        [80.Backup.Model.Points].[alg] = src.[alg],
        [80.Backup.Model.Points].[backup_id] = src.[backup_id],
        [80.Backup.Model.Points].[group_id] = src.[group_id],
        [80.Backup.Model.Points].[type] = src.[type],
        [80.Backup.Model.Points].[creation_time] = src.[creation_time],
        [80.Backup.Model.Points].[usn] = src.[usn]
    FROM [80.Backup.Model.Points] INNER JOIN @imported_changes src
    ON [80.Backup.Model.Points].id = src.id    AND [80.Backup.Model.Points].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.Backup.Model.Points]([link_id], [num], [id], [alg], [backup_id], [group_id], [type], [creation_time], [usn], db_instance_id)
    SELECT
        src.[link_id],
        src.[num],
        src.[id],
        src.[alg],
        src.[backup_id],
        src.[group_id],
        src.[type],
        src.[creation_time],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Backup.Model.Points] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_Points_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_Points_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_Points_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_Points_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Backup.Model.Points]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Backup.Model.Points].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Ssh_creds_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Ssh_creds_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Ssh_creds_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Ssh_creds_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [adjust_firewall] bit,
       [isftserver] bit,
       [endftport] int,
       [auto_sudo] bit,
       [port] int,
       [enabled] bit,
       [startftport] int,
       [timeout] int,
       [elevatetoroot] bit,
       [buffersize] int,
       [id] uniqueidentifier,
       [creds] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([adjust_firewall], [isftserver], [endftport], [auto_sudo], [port], [enabled], [startftport], [timeout], [elevatetoroot], [buffersize], [id], [creds], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [adjust_firewall] bit 'adjust_firewall',
       [isftserver] bit 'isftserver',
       [endftport] int 'endftport',
       [auto_sudo] bit 'auto_sudo',
       [port] int 'port',
       [enabled] bit 'enabled',
       [startftport] int 'startftport',
       [timeout] int 'timeout',
       [elevatetoroot] bit 'elevatetoroot',
       [buffersize] int 'buffersize',
       [id] uniqueidentifier 'id',
       [creds] uniqueidentifier 'creds',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Ssh_creds]
    SET
        [80.Ssh_creds].[adjust_firewall] = src.[adjust_firewall],
        [80.Ssh_creds].[isftserver] = src.[isftserver],
        [80.Ssh_creds].[endftport] = src.[endftport],
        [80.Ssh_creds].[auto_sudo] = src.[auto_sudo],
        [80.Ssh_creds].[port] = src.[port],
        [80.Ssh_creds].[enabled] = src.[enabled],
        [80.Ssh_creds].[startftport] = src.[startftport],
        [80.Ssh_creds].[timeout] = src.[timeout],
        [80.Ssh_creds].[elevatetoroot] = src.[elevatetoroot],
        [80.Ssh_creds].[buffersize] = src.[buffersize],
        [80.Ssh_creds].[creds] = src.[creds],
        [80.Ssh_creds].[usn] = src.[usn]
    FROM [80.Ssh_creds] INNER JOIN @imported_changes src
    ON [80.Ssh_creds].id = src.id    AND [80.Ssh_creds].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.Ssh_creds]([adjust_firewall], [isftserver], [endftport], [auto_sudo], [port], [enabled], [startftport], [timeout], [elevatetoroot], [buffersize], [id], [creds], [usn], db_instance_id)
    SELECT
        src.[adjust_firewall],
        src.[isftserver],
        src.[endftport],
        src.[auto_sudo],
        src.[port],
        src.[enabled],
        src.[startftport],
        src.[timeout],
        src.[elevatetoroot],
        src.[buffersize],
        src.[id],
        src.[creds],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Ssh_creds] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Ssh_creds_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Ssh_creds_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Ssh_creds_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Ssh_creds_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Ssh_creds]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Ssh_creds].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_OIBs_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_OIBs_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_OIBs_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_OIBs_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [has_index] bit,
       [has_exchange] bit,
       [type] int,
       [is_corrupted] bit,
       [alg] int,
       [creation_time] datetime,
       [is_consistent] bit,
       [storage_id] uniqueidentifier,
       [inside_dir] nvarchar(400),
       [state] int,
       [id] uniqueidentifier,
       [guest_os_info] xml,
       [memory] bigint,
       [point_id] uniqueidentifier,
       [link_id] uniqueidentifier,
       [approx_size] bigint,
       [vmname] nvarchar(1000),
       [process_id] int,
       [object_id] uniqueidentifier,
       [aux_data] xml,
       [display_name] nvarchar(256),
       [original_oib_id] uniqueidentifier,
       [fqdn] nvarchar(255),
       [has_sql] bit,
       [parent_id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([has_index], [has_exchange], [type], [is_corrupted], [alg], [creation_time], [is_consistent], [storage_id], [inside_dir], [state], [id], [guest_os_info], [memory], [point_id], [link_id], [approx_size], [vmname], [process_id], [object_id], [aux_data], [display_name], [original_oib_id], [fqdn], [has_sql], [parent_id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [has_index] bit 'has_index',
       [has_exchange] bit 'has_exchange',
       [type] int 'type',
       [is_corrupted] bit 'is_corrupted',
       [alg] int 'alg',
       [creation_time] datetime 'creation_time',
       [is_consistent] bit 'is_consistent',
       [storage_id] uniqueidentifier 'storage_id',
       [inside_dir] nvarchar(400) 'inside_dir',
       [state] int 'state',
       [id] uniqueidentifier 'id',
       [guest_os_info] xml 'guest_os_info/*',
       [memory] bigint 'memory',
       [point_id] uniqueidentifier 'point_id',
       [link_id] uniqueidentifier 'link_id',
       [approx_size] bigint 'approx_size',
       [vmname] nvarchar(1000) 'vmname',
       [process_id] int 'process_id',
       [object_id] uniqueidentifier 'object_id',
       [aux_data] xml 'aux_data/*',
       [display_name] nvarchar(256) 'display_name',
       [original_oib_id] uniqueidentifier 'original_oib_id',
       [fqdn] nvarchar(255) 'fqdn',
       [has_sql] bit 'has_sql',
       [parent_id] uniqueidentifier 'parent_id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    UPDATE @imported_changes 
	SET [creation_time] = DATEADD( minute, -@TimeZoneShift, [creation_time])
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Backup.Model.OIBs]
    SET
        [80.Backup.Model.OIBs].[has_index] = src.[has_index],
        [80.Backup.Model.OIBs].[has_exchange] = src.[has_exchange],
        [80.Backup.Model.OIBs].[type] = src.[type],
        [80.Backup.Model.OIBs].[is_corrupted] = src.[is_corrupted],
        [80.Backup.Model.OIBs].[alg] = src.[alg],
        [80.Backup.Model.OIBs].[creation_time] = src.[creation_time],
        [80.Backup.Model.OIBs].[is_consistent] = src.[is_consistent],
        [80.Backup.Model.OIBs].[storage_id] = src.[storage_id],
        [80.Backup.Model.OIBs].[inside_dir] = src.[inside_dir],
        [80.Backup.Model.OIBs].[state] = src.[state],
        [80.Backup.Model.OIBs].[guest_os_info] = src.[guest_os_info],
        [80.Backup.Model.OIBs].[memory] = src.[memory],
        [80.Backup.Model.OIBs].[point_id] = src.[point_id],
        [80.Backup.Model.OIBs].[link_id] = src.[link_id],
        [80.Backup.Model.OIBs].[approx_size] = src.[approx_size],
        [80.Backup.Model.OIBs].[vmname] = src.[vmname],
        [80.Backup.Model.OIBs].[process_id] = src.[process_id],
        [80.Backup.Model.OIBs].[object_id] = src.[object_id],
        [80.Backup.Model.OIBs].[aux_data] = src.[aux_data],
        [80.Backup.Model.OIBs].[display_name] = src.[display_name],
        [80.Backup.Model.OIBs].[original_oib_id] = src.[original_oib_id],
        [80.Backup.Model.OIBs].[fqdn] = src.[fqdn],
        [80.Backup.Model.OIBs].[has_sql] = src.[has_sql],
        [80.Backup.Model.OIBs].[parent_id] = src.[parent_id],
        [80.Backup.Model.OIBs].[usn] = src.[usn]
    FROM [80.Backup.Model.OIBs] INNER JOIN @imported_changes src
    ON [80.Backup.Model.OIBs].id = src.id    AND [80.Backup.Model.OIBs].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.Backup.Model.OIBs]([has_index], [has_exchange], [type], [is_corrupted], [alg], [creation_time], [is_consistent], [storage_id], [inside_dir], [state], [id], [guest_os_info], [memory], [point_id], [link_id], [approx_size], [vmname], [process_id], [object_id], [aux_data], [display_name], [original_oib_id], [fqdn], [has_sql], [parent_id], [usn], db_instance_id)
    SELECT
        src.[has_index],
        src.[has_exchange],
        src.[type],
        src.[is_corrupted],
        src.[alg],
        src.[creation_time],
        src.[is_consistent],
        src.[storage_id],
        src.[inside_dir],
        src.[state],
        src.[id],
        src.[guest_os_info],
        src.[memory],
        src.[point_id],
        src.[link_id],
        src.[approx_size],
        src.[vmname],
        src.[process_id],
        src.[object_id],
        src.[aux_data],
        src.[display_name],
        src.[original_oib_id],
        src.[fqdn],
        src.[has_sql],
        src.[parent_id],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Backup.Model.OIBs] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_OIBs_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_OIBs_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_OIBs_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_OIBs_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Backup.Model.OIBs]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Backup.Model.OIBs].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_LicensedHosts_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_LicensedHosts_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_LicensedHosts_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_LicensedHosts_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [lic_edition] int,
       [is_licensed] bit,
       [type] int,
       [id] uniqueidentifier,
       [physical_host_id] uniqueidentifier,
       [lic_tier] int,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([lic_edition], [is_licensed], [type], [id], [physical_host_id], [lic_tier], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [lic_edition] int 'lic_edition',
       [is_licensed] bit 'is_licensed',
       [type] int 'type',
       [id] uniqueidentifier 'id',
       [physical_host_id] uniqueidentifier 'physical_host_id',
       [lic_tier] int 'lic_tier',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.LicensedHosts]
    SET
        [80.LicensedHosts].[lic_edition] = src.[lic_edition],
        [80.LicensedHosts].[is_licensed] = src.[is_licensed],
        [80.LicensedHosts].[type] = src.[type],
        [80.LicensedHosts].[physical_host_id] = src.[physical_host_id],
        [80.LicensedHosts].[lic_tier] = src.[lic_tier],
        [80.LicensedHosts].[usn] = src.[usn]
    FROM [80.LicensedHosts] INNER JOIN @imported_changes src
    ON [80.LicensedHosts].id = src.id    AND [80.LicensedHosts].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.LicensedHosts]([lic_edition], [is_licensed], [type], [id], [physical_host_id], [lic_tier], [usn], db_instance_id)
    SELECT
        src.[lic_edition],
        src.[is_licensed],
        src.[type],
        src.[id],
        src.[physical_host_id],
        src.[lic_tier],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.LicensedHosts] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_LicensedHosts_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_LicensedHosts_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_LicensedHosts_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_LicensedHosts_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.LicensedHosts]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.LicensedHosts].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Hosts_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Hosts_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Hosts_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Hosts_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [description] nvarchar(1024),
       [protocol] int,
       [info] nvarchar(255),
       [ip] nvarchar(50),
       [parent_id] uniqueidentifier,
       [api_version] int,
       [type] int,
       [win_creds] xml,
       [id] uniqueidentifier,
       [name] nvarchar(255),
       [reference] nvarchar(1024),
       [host_instance_id] varchar(255),
       [physical_host_id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([description], [protocol], [info], [ip], [parent_id], [api_version], [type], [win_creds], [id], [name], [reference], [host_instance_id], [physical_host_id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [description] nvarchar(1024) 'description',
       [protocol] int 'protocol',
       [info] nvarchar(255) 'info',
       [ip] nvarchar(50) 'ip',
       [parent_id] uniqueidentifier 'parent_id',
       [api_version] int 'api_version',
       [type] int 'type',
       [win_creds] xml 'win_creds',
       [id] uniqueidentifier 'id',
       [name] nvarchar(255) 'name',
       [reference] nvarchar(1024) 'reference',
       [host_instance_id] varchar(255) 'host_instance_id',
       [physical_host_id] uniqueidentifier 'physical_host_id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
        /*  UPDATE HOST INSTANCE IF CHANGED */
      declare @changed_host_instance_id_rows table ( old_id varchar(255), new_id varchar(255));

      INSERT INTO @changed_host_instance_id_rows (old_id, new_id)
      SELECT  [C.Hosts].[host_instance_id], src.[host_instance_id]
      FROM  [C.Hosts] INNER JOIN @imported_changes src ON [C.Hosts].id = src.id
      WHERE [C.Hosts].[host_instance_id] != src.[host_instance_id] COLLATE Latin1_General_CS_AS 
      GROUP BY [C.Hosts].[host_instance_id], src.[host_instance_id]

  /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Hosts]
    SET
        [80.Hosts].[description] = src.[description],
        [80.Hosts].[protocol] = src.[protocol],
        [80.Hosts].[info] = src.[info],
        [80.Hosts].[ip] = src.[ip],
        [80.Hosts].[parent_id] = src.[parent_id],
        [80.Hosts].[api_version] = src.[api_version],
        [80.Hosts].[type] = src.[type],
        [80.Hosts].[win_creds] = src.[win_creds],
        [80.Hosts].[name] = src.[name],
        [80.Hosts].[reference] = src.[reference],
        [80.Hosts].[host_instance_id] = src.[host_instance_id],
        [80.Hosts].[physical_host_id] = src.[physical_host_id],
        [80.Hosts].[usn] = src.[usn]
    FROM [80.Hosts] INNER JOIN @imported_changes src
    ON [80.Hosts].id = src.id    AND [80.Hosts].db_instance_id = @dbInstanceId

       /* UPDATE hierarchy scope */

     UPDATE [dbo].[Security.HierarchyScopes]
     SET [hierarchy_root_instance_id] = changed.new_id,
     [hierarchy_object_keyhash] = [dbo].[fn.Backup.MakeObjectHash]([hierarchy_object_ref], changed.new_id),
     [scope_state] = 0
     FROM   [dbo].[Security.HierarchyScopes]  INNER JOIN @changed_host_instance_id_rows changed 
     ON ([dbo].[Security.HierarchyScopes].hierarchy_root_instance_id = changed.old_id)

  /*      INSERT NEW ROWS       */
    INSERT INTO [80.Hosts]([description], [protocol], [info], [ip], [parent_id], [api_version], [type], [win_creds], [id], [name], [reference], [host_instance_id], [physical_host_id], [usn], db_instance_id)
    SELECT
        src.[description],
        src.[protocol],
        src.[info],
        src.[ip],
        src.[parent_id],
        src.[api_version],
        src.[type],
        src.[win_creds],
        src.[id],
        src.[name],
        src.[reference],
        src.[host_instance_id],
        src.[physical_host_id],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Hosts] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Hosts_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Hosts_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Hosts_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Hosts_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Hosts]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Hosts].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Folders_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Folders_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Folders_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Folders_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [name] nvarchar(255),
       [parent_id] uniqueidentifier,
       [id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([name], [parent_id], [id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [name] nvarchar(255) 'name',
       [parent_id] uniqueidentifier 'parent_id',
       [id] uniqueidentifier 'id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Folders]
    SET
        [80.Folders].[name] = src.[name],
        [80.Folders].[parent_id] = src.[parent_id],
        [80.Folders].[usn] = src.[usn]
    FROM [80.Folders] INNER JOIN @imported_changes src
    ON [80.Folders].id = src.id    AND [80.Folders].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.Folders]([name], [parent_id], [id], [usn], db_instance_id)
    SELECT
        src.[name],
        src.[parent_id],
        src.[id],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Folders] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Folders_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Folders_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Folders_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Folders_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Folders]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Folders].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_VirtualLabs_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_VirtualLabs_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_VirtualLabs_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_VirtualLabs_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [host_id] uniqueidentifier,
       [vm_ref] nvarchar(1024),
       [description] nvarchar(255),
       [name] nvarchar(255),
       [id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([host_id], [vm_ref], [description], [name], [id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [host_id] uniqueidentifier 'host_id',
       [vm_ref] nvarchar(1024) 'vm_ref',
       [description] nvarchar(255) 'description',
       [name] nvarchar(255) 'name',
       [id] uniqueidentifier 'id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.VirtualLabs]
    SET
        [80.VirtualLabs].[host_id] = src.[host_id],
        [80.VirtualLabs].[vm_ref] = src.[vm_ref],
        [80.VirtualLabs].[description] = src.[description],
        [80.VirtualLabs].[name] = src.[name],
        [80.VirtualLabs].[usn] = src.[usn]
    FROM [80.VirtualLabs] INNER JOIN @imported_changes src
    ON [80.VirtualLabs].id = src.id    AND [80.VirtualLabs].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.VirtualLabs]([host_id], [vm_ref], [description], [name], [id], [usn], db_instance_id)
    SELECT
        src.[host_id],
        src.[vm_ref],
        src.[description],
        src.[name],
        src.[id],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.VirtualLabs] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_VirtualLabs_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_VirtualLabs_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_VirtualLabs_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_VirtualLabs_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.VirtualLabs]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.VirtualLabs].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_ObjectsInApplicationGroups_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_ObjectsInApplicationGroups_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_ObjectsInApplicationGroups_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_ObjectsInApplicationGroups_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [effective_memory] bigint,
       [guest_os_info] xml,
       [options] xml,
       [id] uniqueidentifier,
       [order] int,
       [object_id] uniqueidentifier,
       [folder_id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([effective_memory], [guest_os_info], [options], [id], [order], [object_id], [folder_id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [effective_memory] bigint 'effective_memory',
       [guest_os_info] xml 'guest_os_info',
       [options] xml 'options/*',
       [id] uniqueidentifier 'id',
       [order] int 'order',
       [object_id] uniqueidentifier 'object_id',
       [folder_id] uniqueidentifier 'folder_id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.ObjectsInApplicationGroups]
    SET
        [80.ObjectsInApplicationGroups].[effective_memory] = src.[effective_memory],
        [80.ObjectsInApplicationGroups].[guest_os_info] = src.[guest_os_info],
        [80.ObjectsInApplicationGroups].[options] = src.[options],
        [80.ObjectsInApplicationGroups].[order] = src.[order],
        [80.ObjectsInApplicationGroups].[object_id] = src.[object_id],
        [80.ObjectsInApplicationGroups].[folder_id] = src.[folder_id],
        [80.ObjectsInApplicationGroups].[usn] = src.[usn]
    FROM [80.ObjectsInApplicationGroups] INNER JOIN @imported_changes src
    ON [80.ObjectsInApplicationGroups].id = src.id    AND [80.ObjectsInApplicationGroups].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.ObjectsInApplicationGroups]([effective_memory], [guest_os_info], [options], [id], [order], [object_id], [folder_id], [usn], db_instance_id)
    SELECT
        src.[effective_memory],
        src.[guest_os_info],
        src.[options],
        src.[id],
        src.[order],
        src.[object_id],
        src.[folder_id],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.ObjectsInApplicationGroups] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_ObjectsInApplicationGroups_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_ObjectsInApplicationGroups_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_ObjectsInApplicationGroups_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_ObjectsInApplicationGroups_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.ObjectsInApplicationGroups]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.ObjectsInApplicationGroups].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_DRRoles_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_DRRoles_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_DRRoles_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_DRRoles_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [effective_memory] bigint,
       [id] uniqueidentifier,
       [boot_delay] int,
       [test_script_file_full_name] nvarchar(max),
       [name] nvarchar(max),
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([effective_memory], [id], [boot_delay], [test_script_file_full_name], [name], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [effective_memory] bigint 'effective_memory',
       [id] uniqueidentifier 'id',
       [boot_delay] int 'boot_delay',
       [test_script_file_full_name] nvarchar(max) 'test_script_file_full_name',
       [name] nvarchar(max) 'name',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.DRRoles]
    SET
        [80.DRRoles].[effective_memory] = src.[effective_memory],
        [80.DRRoles].[boot_delay] = src.[boot_delay],
        [80.DRRoles].[test_script_file_full_name] = src.[test_script_file_full_name],
        [80.DRRoles].[name] = src.[name],
        [80.DRRoles].[usn] = src.[usn]
    FROM [80.DRRoles] INNER JOIN @imported_changes src
    ON [80.DRRoles].id = src.id    AND [80.DRRoles].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.DRRoles]([effective_memory], [id], [boot_delay], [test_script_file_full_name], [name], [usn], db_instance_id)
    SELECT
        src.[effective_memory],
        src.[id],
        src.[boot_delay],
        src.[test_script_file_full_name],
        src.[name],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.DRRoles] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_DRRoles_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_DRRoles_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_DRRoles_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_DRRoles_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.DRRoles]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.DRRoles].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_JobSessions_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_JobSessions_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_JobSessions_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_JobSessions_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [log_xml] xml,
       [progress] int,
       [job_name] nvarchar(255),
       [result] int,
       [job_id] uniqueidentifier,
       [state] int,
       [job_type] int,
       [creation_time] datetime,
       [id] uniqueidentifier,
       [end_time] datetime,
       [operation] nvarchar(400),
       [control] int,
       [description] text,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([log_xml], [progress], [job_name], [result], [job_id], [state], [job_type], [creation_time], [id], [end_time], [operation], [control], [description], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [log_xml] xml 'log_xml/*',
       [progress] int 'progress',
       [job_name] nvarchar(255) 'job_name',
       [result] int 'result',
       [job_id] uniqueidentifier 'job_id',
       [state] int 'state',
       [job_type] int 'job_type',
       [creation_time] datetime 'creation_time',
       [id] uniqueidentifier 'id',
       [end_time] datetime 'end_time',
       [operation] nvarchar(400) 'operation',
       [control] int 'control',
       [description] text 'description',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc

	MERGE [80.Backup.Model.JobSessions] tgt
	USING @imported_changes src ON tgt.id = src.id AND tgt.db_instance_id = @dbInstanceId
	WHEN MATCHED THEN
		UPDATE SET
			tgt.[log_xml] = src.[log_xml],
			tgt.[progress] = src.[progress],
			tgt.[job_name] = src.[job_name],
			tgt.[result] = src.[result],
			tgt.[job_id] = src.[job_id],
			tgt.[state] = src.[state],
			tgt.[job_type] = src.[job_type],
			tgt.[creation_time] = DATEADD(minute, -@TimeZoneShift, src.[creation_time]),
			tgt.[end_time] = DATEADD(minute, -@TimeZoneShift, src.[end_time]),
			tgt.[operation] = src.[operation],
			tgt.[control] = src.[control],
			tgt.[description] = src.[description],
			tgt.[usn] = src.[usn]
	WHEN NOT MATCHED BY TARGET THEN
		INSERT ([log_xml], [progress], [job_name], [result], [job_id], [state], [job_type], [creation_time], [id], [end_time], [operation], [control], [description], [usn], db_instance_id)
		VALUES (
			src.[log_xml],
			src.[progress],
			src.[job_name],
			src.[result],
			src.[job_id],
			src.[state],
			src.[job_type],
			DATEADD(minute, -@TimeZoneShift, src.[creation_time]),
			src.[id],
			DATEADD(minute, -@TimeZoneShift, src.[end_time]),
			src.[operation],
			src.[control],
			src.[description],
			src.[usn],
			@dbInstanceId
		)
	OUTPUT
		inserted.[id] AS [id],
		CASE $action WHEN 'INSERT' THEN 0 ELSE 1 END AS [change_type];
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_JobSessions_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_JobSessions_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_JobSessions_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_JobSessions_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Backup.Model.JobSessions]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Backup.Model.JobSessions].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_SbTaskSessions_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_SbTaskSessions_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_SbTaskSessions_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_SbTaskSessions_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [order_num] int,
       [object_id] uniqueidentifier,
       [description] nvarchar(max),
       [powerOn_status] int,
       [restore_counter] int,
       [ping_status] int,
       [appliance_ip] nvarchar(80),
       [subnet_ip] nvarchar(80),
       [subnet_mask] nvarchar(80),
       [vm_ref] nvarchar(max),
       [state] int,
       [start_time] datetime,
       [drsession_id] uniqueidentifier,
       [total_steps] int,
       [percent] int,
       [finish_time] datetime,
       [object_name] nvarchar(max),
       [id] uniqueidentifier,
       [oib_id] uniqueidentifier,
       [heartbeat_status] int,
       [overall_status] int,
       [control] int,
       [type] int,
       [test_script_error_code] int,
       [powerOff_status] int,
       [vm_external_ip] nvarchar(80),
       [test_script_status] int,
       [test_scripts_results] xml,
       [oiag_id] uniqueidentifier,
       [processed_steps] int,
       [leave_powered_on] bit,
       [validation_status] int,
       [log_xml] xml,
       [usn] bigint
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([order_num], [object_id], [description], [powerOn_status], [restore_counter], [ping_status], [appliance_ip], [subnet_ip], [subnet_mask], [vm_ref], [state], [start_time], [drsession_id], [total_steps], [percent], [finish_time], [object_name], [id], [oib_id], [heartbeat_status], [overall_status], [control], [type], [test_script_error_code], [powerOff_status], [vm_external_ip], [test_script_status], [test_scripts_results], [oiag_id], [processed_steps], [leave_powered_on], [validation_status], [log_xml], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [order_num] int 'order_num',
       [object_id] uniqueidentifier 'object_id',
       [description] nvarchar(max) 'description',
       [powerOn_status] int 'powerOn_status',
       [restore_counter] int 'restore_counter',
       [ping_status] int 'ping_status',
       [appliance_ip] nvarchar(80) 'appliance_ip',
       [subnet_ip] nvarchar(80) 'subnet_ip',
       [subnet_mask] nvarchar(80) 'subnet_mask',
       [vm_ref] nvarchar(max) 'vm_ref',
       [state] int 'state',
       [start_time] datetime 'start_time',
       [drsession_id] uniqueidentifier 'drsession_id',
       [total_steps] int 'total_steps',
       [percent] int 'percent',
       [finish_time] datetime 'finish_time',
       [object_name] nvarchar(max) 'object_name',
       [id] uniqueidentifier 'id',
       [oib_id] uniqueidentifier 'oib_id',
       [heartbeat_status] int 'heartbeat_status',
       [overall_status] int 'overall_status',
       [control] int 'control',
       [type] int 'type',
       [test_script_error_code] int 'test_script_error_code',
       [powerOff_status] int 'powerOff_status',
       [vm_external_ip] nvarchar(80) 'vm_external_ip',
       [test_script_status] int 'test_script_status',
       [test_scripts_results] xml 'test_scripts_results/*',
       [oiag_id] uniqueidentifier 'oiag_id',
       [processed_steps] int 'processed_steps',
       [leave_powered_on] bit 'leave_powered_on',
       [validation_status] int 'validation_status',
       [log_xml] xml 'log_xml/*',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc
	
	MERGE [80.Backup.Model.SbTaskSessions] tgt
	USING @imported_changes src ON tgt.id = src.id AND tgt.db_instance_id = @dbInstanceId
	WHEN MATCHED THEN
		UPDATE SET
			tgt.[order_num] = src.[order_num],
			tgt.[object_id] = src.[object_id],
			tgt.[description] = src.[description],
			tgt.[powerOn_status] = src.[powerOn_status],
			tgt.[restore_counter] = src.[restore_counter],
			tgt.[ping_status] = src.[ping_status],
			tgt.[appliance_ip] = src.[appliance_ip],
			tgt.[subnet_ip] = src.[subnet_ip],
			tgt.[subnet_mask] = src.[subnet_mask],
			tgt.[vm_ref] = src.[vm_ref],
			tgt.[state] = src.[state],
			tgt.[start_time] = DATEADD(minute, -@TimeZoneShift, src.[start_time]),
			tgt.[drsession_id] = src.[drsession_id],
			tgt.[total_steps] = src.[total_steps],
			tgt.[percent] = src.[percent],
			tgt.[finish_time] = DATEADD(minute, -@TimeZoneShift, src.[finish_time]),
			tgt.[object_name] = src.[object_name],
			tgt.[oib_id] = src.[oib_id],
			tgt.[heartbeat_status] = src.[heartbeat_status],
			tgt.[overall_status] = src.[overall_status],
			tgt.[control] = src.[control],
			tgt.[type] = src.[type],
			tgt.[test_script_error_code] = src.[test_script_error_code],
			tgt.[powerOff_status] = src.[powerOff_status],
			tgt.[vm_external_ip] = src.[vm_external_ip],
			tgt.[test_script_status] = src.[test_script_status],
			tgt.[test_scripts_results] = src.[test_scripts_results],
			tgt.[oiag_id] = src.[oiag_id],
			tgt.[processed_steps] = src.[processed_steps],
			tgt.[leave_powered_on] = src.[leave_powered_on],
			tgt.[validation_status] = src.[validation_status],
			tgt.[log_xml] = src.[log_xml],
			tgt.[usn] = src.[usn]
	WHEN NOT MATCHED BY TARGET THEN
		INSERT ([order_num], [object_id], [description], [powerOn_status], [restore_counter], [ping_status], [appliance_ip], [subnet_ip], [subnet_mask], [vm_ref], [state], [start_time], [drsession_id], [total_steps], [percent], [finish_time], [object_name], [id], [oib_id], [heartbeat_status], [overall_status], [control], [type], [test_script_error_code], [powerOff_status], [vm_external_ip], [test_script_status], [test_scripts_results], [oiag_id], [processed_steps], [leave_powered_on], [validation_status], [log_xml], [usn], db_instance_id)
		VALUES (
			src.[order_num],
			src.[object_id],
			src.[description],
			src.[powerOn_status],
			src.[restore_counter],
			src.[ping_status],
			src.[appliance_ip],
			src.[subnet_ip],
			src.[subnet_mask],
			src.[vm_ref],
			src.[state],
			DATEADD(minute, -@TimeZoneShift, src.[start_time]),
			src.[drsession_id],
			src.[total_steps],
			src.[percent],
			DATEADD(minute, -@TimeZoneShift, src.[finish_time]),
			src.[object_name],
			src.[id],
			src.[oib_id],
			src.[heartbeat_status],
			src.[overall_status],
			src.[control],
			src.[type],
			src.[test_script_error_code],
			src.[powerOff_status],
			src.[vm_external_ip],
			src.[test_script_status],
			src.[test_scripts_results],
			src.[oiag_id],
			src.[processed_steps],
			src.[leave_powered_on],
			src.[validation_status],
			src.[log_xml],
			src.[usn],
			@dbInstanceId
		)
	OUTPUT
		inserted.[id] AS [id],
		CASE $action WHEN 'INSERT' THEN 0 ELSE 1 END AS [change_type];
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_SbTaskSessions_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_SbTaskSessions_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_SbTaskSessions_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_SbTaskSessions_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Backup.Model.SbTaskSessions]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Backup.Model.SbTaskSessions].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_SbSessions_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_SbSessions_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_SbSessions_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_SbSessions_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [preferred_date] datetime,
       [id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([preferred_date], [id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [preferred_date] datetime 'preferred_date',
       [id] uniqueidentifier 'id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    UPDATE @imported_changes 
	SET [preferred_date] = DATEADD( minute, -@TimeZoneShift, [preferred_date])
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Backup.Model.SbSessions]
    SET
        [80.Backup.Model.SbSessions].[preferred_date] = src.[preferred_date],
        [80.Backup.Model.SbSessions].[usn] = src.[usn]
    FROM [80.Backup.Model.SbSessions] INNER JOIN @imported_changes src
    ON [80.Backup.Model.SbSessions].id = src.id    AND [80.Backup.Model.SbSessions].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.Backup.Model.SbSessions]([preferred_date], [id], [usn], db_instance_id)
    SELECT
        src.[preferred_date],
        src.[id],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Backup.Model.SbSessions] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_SbSessions_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_SbSessions_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_SbSessions_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_SbSessions_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Backup.Model.SbSessions]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Backup.Model.SbSessions].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_BackupJobSessions_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_BackupJobSessions_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_BackupJobSessions_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_BackupJobSessions_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [is_full] bit,
       [total_size] bigint,
       [processed_objects] int,
       [is_retry] bit,
       [total_objects] int,
       [processed_size] bigint,
       [avg_speed] bigint,
       [job_source_type] int,
       [id] uniqueidentifier,
       [stored_size] bigint,
       [is_active_full] bit,
       [cur_point_id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([is_full], [total_size], [processed_objects], [is_retry], [total_objects], [processed_size], [avg_speed], [job_source_type], [id], [stored_size], [is_active_full], [cur_point_id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [is_full] bit 'is_full',
       [total_size] bigint 'total_size',
       [processed_objects] int 'processed_objects',
       [is_retry] bit 'is_retry',
       [total_objects] int 'total_objects',
       [processed_size] bigint 'processed_size',
       [avg_speed] bigint 'avg_speed',
       [job_source_type] int 'job_source_type',
       [id] uniqueidentifier 'id',
       [stored_size] bigint 'stored_size',
       [is_active_full] bit 'is_active_full',
       [cur_point_id] uniqueidentifier 'cur_point_id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc

	MERGE [80.Backup.Model.BackupJobSessions] tgt
	USING @imported_changes src ON tgt.id = src.id AND tgt.db_instance_id = @dbInstanceId
	WHEN MATCHED THEN
		UPDATE SET
			tgt.[is_full] = src.[is_full],
			tgt.[total_size] = src.[total_size],
			tgt.[processed_objects] = src.[processed_objects],
			tgt.[is_retry] = src.[is_retry],
			tgt.[total_objects] = src.[total_objects],
			tgt.[processed_size] = src.[processed_size],
			tgt.[avg_speed] = src.[avg_speed],
			tgt.[job_source_type] = src.[job_source_type],
			tgt.[stored_size] = src.[stored_size],
			tgt.[is_active_full] = src.[is_active_full],
			tgt.[cur_point_id] = src.[cur_point_id],
			tgt.[usn] = src.[usn]
	WHEN NOT MATCHED BY TARGET THEN
		INSERT ([is_full], [total_size], [processed_objects], [is_retry], [total_objects], [processed_size], [avg_speed], [job_source_type], [id], [stored_size], [is_active_full], [cur_point_id], [usn], db_instance_id)
		VALUES (
			src.[is_full],
			src.[total_size],
			src.[processed_objects],
			src.[is_retry],
			src.[total_objects],
			src.[processed_size],
			src.[avg_speed],
			src.[job_source_type],
			src.[id],
			src.[stored_size],
			src.[is_active_full],
			src.[cur_point_id],
			src.[usn],
			@dbInstanceId
		)
	OUTPUT
		inserted.[id] AS [id],
		CASE $action WHEN 'INSERT' THEN 0 ELSE 1 END AS [change_type];
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_BackupJobSessions_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_BackupJobSessions_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_BackupJobSessions_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_BackupJobSessions_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Backup.Model.BackupJobSessions]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Backup.Model.BackupJobSessions].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_BackupTaskSessions_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_BackupTaskSessions_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_BackupTaskSessions_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_BackupTaskSessions_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [processed_size] bigint,
       [operation] nvarchar(400),
       [end_time] datetime,
       [object_name] nvarchar(2000),
       [log_xml] xml,
       [read_size] bigint,
       [stored_size] bigint,
       [object_id] uniqueidentifier,
       [processed_objects] int,
       [session_id] uniqueidentifier,
       [creation_time] datetime,
       [mode] int,
       [total_objects] int,
       [total_size] bigint,
       [reason] ntext,
       [change_tracking] bit,
       [status] int,
       [avg_speed] bigint,
       [usn] bigint
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [processed_size], [operation], [end_time], [object_name], [log_xml], [read_size], [stored_size], [object_id], [processed_objects], [session_id], [creation_time], [mode], [total_objects], [total_size], [reason], [change_tracking], [status], [avg_speed], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [processed_size] bigint 'processed_size',
       [operation] nvarchar(400) 'operation',
       [end_time] datetime 'end_time',
       [object_name] nvarchar(2000) 'object_name',
       [log_xml] xml 'log_xml/*',
       [read_size] bigint 'read_size',
       [stored_size] bigint 'stored_size',
       [object_id] uniqueidentifier 'object_id',
       [processed_objects] int 'processed_objects',
       [session_id] uniqueidentifier 'session_id',
       [creation_time] datetime 'creation_time',
       [mode] int 'mode',
       [total_objects] int 'total_objects',
       [total_size] bigint 'total_size',
       [reason] ntext 'reason',
       [change_tracking] bit 'change_tracking',
       [status] int 'status',
       [avg_speed] bigint 'avg_speed',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc

	MERGE [80.Backup.Model.BackupTaskSessions] tgt
	USING @imported_changes src ON tgt.id = src.id AND tgt.db_instance_id = @dbInstanceId
	WHEN MATCHED THEN
		UPDATE SET
			tgt.[processed_size] = src.[processed_size],
			tgt.[operation] = src.[operation],
			tgt.[end_time] = DATEADD(minute, -@TimeZoneShift, src.[end_time]),
			tgt.[object_name] = src.[object_name],
			tgt.[log_xml] = src.[log_xml],
			tgt.[read_size] = src.[read_size],
			tgt.[stored_size] = src.[stored_size],
			tgt.[object_id] = src.[object_id],
			tgt.[processed_objects] = src.[processed_objects],
			tgt.[session_id] = src.[session_id],
			tgt.[creation_time] = DATEADD(minute, -@TimeZoneShift, src.[creation_time]),
			tgt.[mode] = src.[mode],
			tgt.[total_objects] = src.[total_objects],
			tgt.[total_size] = src.[total_size],
			tgt.[reason] = src.[reason],
			tgt.[change_tracking] = src.[change_tracking],
			tgt.[status] = src.[status],
			tgt.[avg_speed] = src.[avg_speed],
			tgt.[usn] = src.[usn]
	WHEN NOT MATCHED BY TARGET THEN
		INSERT ([id], [processed_size], [operation], [end_time], [object_name], [log_xml], [read_size], [stored_size], [object_id], [processed_objects], [session_id], [creation_time], [mode], [total_objects], [total_size], [reason], [change_tracking], [status], [avg_speed], [usn], db_instance_id)
		VALUES (
			src.[id],
			src.[processed_size],
			src.[operation],
			DATEADD(minute, -@TimeZoneShift, src.[end_time]),
			src.[object_name],
			src.[log_xml],
			src.[read_size],
			src.[stored_size],
			src.[object_id],
			src.[processed_objects],
			src.[session_id],
			DATEADD(minute, -@TimeZoneShift, src.[creation_time]),
			src.[mode],
			src.[total_objects],
			src.[total_size],
			src.[reason],
			src.[change_tracking],
			src.[status],
			src.[avg_speed],
			src.[usn],
			@dbInstanceId
		)
	OUTPUT
		inserted.[id] AS [id],
		CASE $action WHEN 'INSERT' THEN 0 ELSE 1 END AS [change_type];
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_BackupTaskSessions_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_BackupTaskSessions_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_BackupTaskSessions_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_BackupTaskSessions_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Backup.Model.BackupTaskSessions]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Backup.Model.BackupTaskSessions].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_LinkedJobs_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_LinkedJobs_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_LinkedJobs_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_LinkedJobs_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [linked_job_id] uniqueidentifier,
       [id] uniqueidentifier,
       [job_id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([linked_job_id], [id], [job_id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [linked_job_id] uniqueidentifier 'linked_job_id',
       [id] uniqueidentifier 'id',
       [job_id] uniqueidentifier 'job_id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.LinkedJobs]
    SET
        [80.LinkedJobs].[linked_job_id] = src.[linked_job_id],
        [80.LinkedJobs].[job_id] = src.[job_id],
        [80.LinkedJobs].[usn] = src.[usn]
    FROM [80.LinkedJobs] INNER JOIN @imported_changes src
    ON [80.LinkedJobs].id = src.id    AND [80.LinkedJobs].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.LinkedJobs]([linked_job_id], [id], [job_id], [usn], db_instance_id)
    SELECT
        src.[linked_job_id],
        src.[id],
        src.[job_id],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.LinkedJobs] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_LinkedJobs_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_LinkedJobs_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_LinkedJobs_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_LinkedJobs_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.LinkedJobs]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.LinkedJobs].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_SbVerificationRules_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_SbVerificationRules_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_SbVerificationRules_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_SbVerificationRules_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [options] xml,
       [job_id] uniqueidentifier,
       [object_type] int,
       [object_id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [options], [job_id], [object_type], [object_id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [options] xml 'options/*',
       [job_id] uniqueidentifier 'job_id',
       [object_type] int 'object_type',
       [object_id] uniqueidentifier 'object_id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.SbVerificationRules]
    SET
        [80.SbVerificationRules].[options] = src.[options],
        [80.SbVerificationRules].[job_id] = src.[job_id],
        [80.SbVerificationRules].[object_type] = src.[object_type],
        [80.SbVerificationRules].[object_id] = src.[object_id],
        [80.SbVerificationRules].[usn] = src.[usn]
    FROM [80.SbVerificationRules] INNER JOIN @imported_changes src
    ON [80.SbVerificationRules].id = src.id    AND [80.SbVerificationRules].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.SbVerificationRules]([id], [options], [job_id], [object_type], [object_id], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[options],
        src.[job_id],
        src.[object_type],
        src.[object_id],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.SbVerificationRules] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_SbVerificationRules_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_SbVerificationRules_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_SbVerificationRules_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_SbVerificationRules_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.SbVerificationRules]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.SbVerificationRules].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_BackupProxies_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_BackupProxies_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_BackupProxies_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_BackupProxies_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [name] nvarchar(max),
       [description] nvarchar(max),
       [type] int,
       [options] xml,
       [disabled] bit,
       [is_busy] bit,
       [is_unavailable] bit,
       [unique_id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [name], [description], [type], [options], [disabled], [is_busy], [is_unavailable], [unique_id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [name] nvarchar(max) 'name',
       [description] nvarchar(max) 'description',
       [type] int 'type',
       [options] xml 'options/*',
       [disabled] bit 'disabled',
       [is_busy] bit 'is_busy',
       [is_unavailable] bit 'is_unavailable',
       [unique_id] uniqueidentifier 'unique_id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.BackupProxies]
    SET
        [80.BackupProxies].[name] = src.[name],
        [80.BackupProxies].[description] = src.[description],
        [80.BackupProxies].[type] = src.[type],
        [80.BackupProxies].[options] = src.[options],
        [80.BackupProxies].[disabled] = src.[disabled],
        [80.BackupProxies].[is_busy] = src.[is_busy],
        [80.BackupProxies].[is_unavailable] = src.[is_unavailable],
        [80.BackupProxies].[unique_id] = src.[unique_id],
        [80.BackupProxies].[usn] = src.[usn]
    FROM [80.BackupProxies] INNER JOIN @imported_changes src
    ON [80.BackupProxies].id = src.id    AND [80.BackupProxies].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.BackupProxies]([id], [name], [description], [type], [options], [disabled], [is_busy], [is_unavailable], [unique_id], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[name],
        src.[description],
        src.[type],
        src.[options],
        src.[disabled],
        src.[is_busy],
        src.[is_unavailable],
        src.[unique_id],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.BackupProxies] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_BackupProxies_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_BackupProxies_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_BackupProxies_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_BackupProxies_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.BackupProxies]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.BackupProxies].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_BackupRepositories_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_BackupRepositories_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_BackupRepositories_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_BackupRepositories_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [name] nvarchar(max),
       [description] nvarchar(max),
       [type] int,
       [host_id] uniqueidentifier,
       [mount_host_id] uniqueidentifier,
       [path] nvarchar(max),
       [options] xml,
       [is_busy] bit,
       [is_unavailable] bit,
       [is_full] bit,
       [total_space] bigint,
       [free_space] bigint,
       [unique_id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [name], [description], [type], [host_id], [mount_host_id], [path], [options], [is_busy], [is_unavailable], [is_full], [total_space], [free_space], [unique_id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [name] nvarchar(max) 'name',
       [description] nvarchar(max) 'description',
       [type] int 'type',
       [host_id] uniqueidentifier 'host_id',
       [mount_host_id] uniqueidentifier 'mount_host_id',
       [path] nvarchar(max) 'path',
       [options] xml 'options/*',
       [is_busy] bit 'is_busy',
       [is_unavailable] bit 'is_unavailable',
       [is_full] bit 'is_full',
       [total_space] bigint 'total_space',
       [free_space] bigint 'free_space',
       [unique_id] uniqueidentifier 'unique_id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.BackupRepositories]
    SET
        [80.BackupRepositories].[name] = src.[name],
        [80.BackupRepositories].[description] = src.[description],
        [80.BackupRepositories].[type] = src.[type],
        [80.BackupRepositories].[host_id] = src.[host_id],
        [80.BackupRepositories].[mount_host_id] = src.[mount_host_id],
        [80.BackupRepositories].[path] = src.[path],
        [80.BackupRepositories].[options] = src.[options],
        [80.BackupRepositories].[is_busy] = src.[is_busy],
        [80.BackupRepositories].[is_unavailable] = src.[is_unavailable],
        [80.BackupRepositories].[is_full] = src.[is_full],
        [80.BackupRepositories].[total_space] = src.[total_space],
        [80.BackupRepositories].[free_space] = src.[free_space],
        [80.BackupRepositories].[unique_id] = src.[unique_id],
        [80.BackupRepositories].[usn] = src.[usn]
    FROM [80.BackupRepositories] INNER JOIN @imported_changes src
    ON [80.BackupRepositories].id = src.id    AND [80.BackupRepositories].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.BackupRepositories]([id], [name], [description], [type], [host_id], [mount_host_id], [path], [options], [is_busy], [is_unavailable], [is_full], [total_space], [free_space], [unique_id], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[name],
        src.[description],
        src.[type],
        src.[host_id],
        src.[mount_host_id],
        src.[path],
        src.[options],
        src.[is_busy],
        src.[is_unavailable],
        src.[is_full],
        src.[total_space],
        src.[free_space],
        src.[unique_id],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.BackupRepositories] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_BackupRepositories_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_BackupRepositories_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_BackupRepositories_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_BackupRepositories_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.BackupRepositories]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.BackupRepositories].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Credentials_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Credentials_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Credentials_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Credentials_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [user_name] nvarchar(255),
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [user_name], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [user_name] nvarchar(255) 'user_name',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Credentials]
    SET
        [80.Credentials].[user_name] = src.[user_name],
        [80.Credentials].[usn] = src.[usn]
    FROM [80.Credentials] INNER JOIN @imported_changes src
    ON [80.Credentials].id = src.id    AND [80.Credentials].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.Credentials]([id], [user_name], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[user_name],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Credentials] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Credentials_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Credentials_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Credentials_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Credentials_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Credentials]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Credentials].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------



-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_RestoreJobSessions_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_RestoreJobSessions_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_RestoreJobSessions_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_RestoreJobSessions_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [action] int,
       [usn] bigint,
       [options] xml,
       [initiator_sid] nvarchar(255),
       [initiator_name] nvarchar(255),
       [reason] nvarchar(max),
       [files_log_xml] xml,
       [platform] int,
       [multi_restore_id] uniqueidentifier,
       [restore_type] int,
       [oib_id] uniqueidentifier,
       [is_internal] bit,
       [oib_display_name] nvarchar(256),
       [oib_creation_time] datetime,
       [sub_type] int,
       [restored_obj_id] uniqueidentifier,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [action], [usn], [options], [initiator_sid], [initiator_name], [reason], [files_log_xml], [platform], [multi_restore_id], [restore_type], [oib_id], [is_internal], [oib_display_name], [oib_creation_time], [sub_type], [restored_obj_id])
    SELECT 
       [id],
       [action],
       [usn],
       ISNULL([options], ''),
       [initiator_sid],
       [initiator_name],
       [reason],
       ISNULL([files_log_xml], '<Root TotalUsn="0" TotalId="0" />'),
       [platform],
       [multi_restore_id],
       [restore_type],
       [oib_id],
       [is_internal],
       [oib_display_name],
       [oib_creation_time],
       [sub_type],
       [restored_obj_id]
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [action] int 'action',
       [usn] bigint 'usn',
       [options] xml 'options/*',
       [initiator_sid] nvarchar(255) 'initiator_sid',
       [initiator_name] nvarchar(255) 'initiator_name',
       [reason] nvarchar(max) 'reason',
       [files_log_xml] xml 'files_log_xml/*',
       [platform] int 'platform',
       [multi_restore_id] uniqueidentifier 'multi_restore_id',
       [restore_type] int 'restore_type',
       [oib_id] uniqueidentifier 'oib_id',
       [is_internal] bit 'is_internal',
       [oib_display_name] nvarchar(256) 'oib_display_name',
       [oib_creation_time] datetime 'oib_creation_time',
       [sub_type] int 'sub_type',
       [restored_obj_id] uniqueidentifier 'restored_obj_id'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId
	
	MERGE [80.Backup.Model.RestoreJobSessions] tgt
	USING @imported_changes src ON tgt.id = src.id AND tgt.db_instance_id = @dbInstanceId
	WHEN MATCHED THEN
		UPDATE SET
			tgt.[action] = src.[action],
			tgt.[usn] = src.[usn],
			tgt.[options] = src.[options],
			tgt.[initiator_sid] = src.[initiator_sid],
			tgt.[initiator_name] = src.[initiator_name],
			tgt.[reason] = src.[reason],
			tgt.[files_log_xml] = src.[files_log_xml],
			tgt.[platform] = src.[platform],
			tgt.[multi_restore_id] = src.[multi_restore_id],
			tgt.[restore_type] = src.[restore_type],
			tgt.[oib_id] = src.[oib_id],
			tgt.[is_internal] = src.[is_internal],
			tgt.[oib_display_name] = src.[oib_display_name],
			tgt.[oib_creation_time] = src.[oib_creation_time],
			tgt.[sub_type] = src.[sub_type],
			tgt.[restored_obj_id] = src.[restored_obj_id]
	WHEN NOT MATCHED BY TARGET THEN
		INSERT ([id], [action], [usn], [options], [initiator_sid], [initiator_name], [reason], [files_log_xml], [platform], [multi_restore_id], [restore_type], [oib_id], [is_internal], [oib_display_name], [oib_creation_time], [sub_type], [restored_obj_id], db_instance_id)
		VALUES (
			src.[id],
			src.[action],
			src.[usn],
			src.[options],
			src.[initiator_sid],
			src.[initiator_name],
			src.[reason],
			src.[files_log_xml],
			src.[platform],
			src.[multi_restore_id],
			src.[restore_type],
			src.[oib_id],
			src.[is_internal],
			src.[oib_display_name],
			src.[oib_creation_time],
			src.[sub_type],
			src.[restored_obj_id],
			src.db_instance_id
		)
	OUTPUT
		inserted.[id] AS [id],
		CASE $action WHEN 'INSERT' THEN 0 ELSE 1 END AS [change_type];
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_RestoreJobSessions_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_RestoreJobSessions_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_RestoreJobSessions_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_RestoreJobSessions_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Backup.Model.RestoreJobSessions]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Backup.Model.RestoreJobSessions].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_JobVssCredentials_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_JobVssCredentials_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_JobVssCredentials_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_JobVssCredentials_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [job_id] uniqueidentifier,
       [oij_id] uniqueidentifier,
       [win_creds_id] uniqueidentifier,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [job_id], [oij_id], [win_creds_id], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [job_id] uniqueidentifier 'job_id',
       [oij_id] uniqueidentifier 'oij_id',
       [win_creds_id] uniqueidentifier 'win_creds_id',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.JobVssCredentials]
    SET
        [80.JobVssCredentials].[job_id] = src.[job_id],
        [80.JobVssCredentials].[oij_id] = src.[oij_id],
        [80.JobVssCredentials].[win_creds_id] = src.[win_creds_id],
        [80.JobVssCredentials].[usn] = src.[usn]
    FROM [80.JobVssCredentials] INNER JOIN @imported_changes src
    ON [80.JobVssCredentials].id = src.id    AND [80.JobVssCredentials].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.JobVssCredentials]([id], [job_id], [oij_id], [win_creds_id], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[job_id],
        src.[oij_id],
        src.[win_creds_id],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.JobVssCredentials] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_JobVssCredentials_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_JobVssCredentials_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_JobVssCredentials_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_JobVssCredentials_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.JobVssCredentials]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.JobVssCredentials].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_PhysicalHosts_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_PhysicalHosts_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_PhysicalHosts_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_PhysicalHosts_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [name] nvarchar(max),
       [bios_uuid] nvarchar(255),
       [chassis_type] int NOT NULL,
       [hardware_info] xml NOT NULL,
       [net_info] xml NOT NULL,
       [os_type] int NOT NULL,
       [os_platform] int NOT NULL,
	   [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [name], [bios_uuid], [chassis_type], [hardware_info], [net_info], [os_type], [os_platform], [usn])
    SELECT 
       [id],
       [name],
       [bios_uuid],
       [chassis_type],
       ISNULL([hardware_info], '<HardwareInfo />'),
       ISNULL([net_info], '<NetInfo />'),
       [os_type],
       [os_platform],
	   [usn]
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [name] nvarchar(max) 'name',
       [bios_uuid] nvarchar(255) 'bios_uuid',
       [chassis_type] int 'chassis_type',
       [hardware_info] xml 'hardware_info/*',
       [net_info] xml 'net_info/*',
       [os_type] int 'os_type',
       [os_platform] int 'os_platform',
	   [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.PhysicalHosts]
    SET
        [80.PhysicalHosts].[name] = src.[name],
        [80.PhysicalHosts].[bios_uuid] = src.[bios_uuid],
        [80.PhysicalHosts].[chassis_type] = src.[chassis_type],
        [80.PhysicalHosts].[hardware_info] = src.[hardware_info],
        [80.PhysicalHosts].[net_info] = src.[net_info],
        [80.PhysicalHosts].[os_type] = src.[os_type],
        [80.PhysicalHosts].[os_platform] = src.[os_platform],
        [80.PhysicalHosts].[usn] = src.[usn]
    FROM [80.PhysicalHosts] INNER JOIN @imported_changes src
    ON [80.PhysicalHosts].id = src.id    AND [80.PhysicalHosts].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.PhysicalHosts]([id], [name], [bios_uuid], [chassis_type], [hardware_info], [net_info], [os_type], [os_platform], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[name],
        src.[bios_uuid],
        src.[chassis_type],
        src.[hardware_info],
        src.[net_info],
        src.[os_type],
        src.[os_platform],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.PhysicalHosts] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_PhysicalHosts_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_PhysicalHosts_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_PhysicalHosts_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_PhysicalHosts_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.PhysicalHosts]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.PhysicalHosts].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_RestoreTaskSessions_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_RestoreTaskSessions_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_RestoreTaskSessions_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_RestoreTaskSessions_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [session_id] uniqueidentifier,
       [object_name] nvarchar(255),
       [object_id] uniqueidentifier,
       [status] int,
       [reason] ntext,
       [creation_time] datetime,
       [end_time] datetime,
       [operation] nvarchar(255),
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [session_id], [object_name], [object_id], [status], [reason], [creation_time], [end_time], [operation], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [session_id] uniqueidentifier 'session_id',
       [object_name] nvarchar(255) 'object_name',
       [object_id] uniqueidentifier 'object_id',
       [status] int 'status',
       [reason] ntext 'reason',
       [creation_time] datetime 'creation_time',
       [end_time] datetime 'end_time',
       [operation] nvarchar(255) 'operation',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Backup.Model.RestoreTaskSessions]
    SET
        [80.Backup.Model.RestoreTaskSessions].[session_id] = src.[session_id],
        [80.Backup.Model.RestoreTaskSessions].[object_name] = src.[object_name],
        [80.Backup.Model.RestoreTaskSessions].[object_id] = src.[object_id],
        [80.Backup.Model.RestoreTaskSessions].[status] = src.[status],
        [80.Backup.Model.RestoreTaskSessions].[reason] = src.[reason],
        [80.Backup.Model.RestoreTaskSessions].[creation_time] = src.[creation_time],
        [80.Backup.Model.RestoreTaskSessions].[end_time] = src.[end_time],
        [80.Backup.Model.RestoreTaskSessions].[operation] = src.[operation],
        [80.Backup.Model.RestoreTaskSessions].[usn] = src.[usn]
    FROM [80.Backup.Model.RestoreTaskSessions] INNER JOIN @imported_changes src
    ON [80.Backup.Model.RestoreTaskSessions].id = src.id    AND [80.Backup.Model.RestoreTaskSessions].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.Backup.Model.RestoreTaskSessions]([id], [session_id], [object_name], [object_id], [status], [reason], [creation_time], [end_time], [operation], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[session_id],
        src.[object_name],
        src.[object_id],
        src.[status],
        src.[reason],
        src.[creation_time],
        src.[end_time],
        src.[operation],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Backup.Model.RestoreTaskSessions] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_RestoreTaskSessions_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_RestoreTaskSessions_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_RestoreTaskSessions_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_RestoreTaskSessions_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Backup.Model.RestoreTaskSessions]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Backup.Model.RestoreTaskSessions].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_OibAdminAccounts_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_OibAdminAccounts_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_OibAdminAccounts_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_OibAdminAccounts_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [sid] nvarchar(184),
       [oib_id] uniqueidentifier,
       [account_type] int,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [sid], [oib_id], [account_type], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [sid] nvarchar(184) 'sid',
       [oib_id] uniqueidentifier 'oib_id',
       [account_type] int 'account_type',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.OibAdminAccounts]
    SET
        [80.OibAdminAccounts].[sid] = src.[sid],
        [80.OibAdminAccounts].[oib_id] = src.[oib_id],
        [80.OibAdminAccounts].[account_type] = src.[account_type],
        [80.OibAdminAccounts].[usn] = src.[usn]
    FROM [80.OibAdminAccounts] INNER JOIN @imported_changes src
    ON [80.OibAdminAccounts].id = src.id    AND [80.OibAdminAccounts].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.OibAdminAccounts]([id], [sid], [oib_id], [account_type], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[sid],
        src.[oib_id],
        src.[account_type],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.OibAdminAccounts] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_OibAdminAccounts_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_OibAdminAccounts_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_OibAdminAccounts_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_OibAdminAccounts_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.OibAdminAccounts]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.OibAdminAccounts].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_GuestDatabase_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_GuestDatabase_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_GuestDatabase_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_GuestDatabase_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [obj_id] uniqueidentifier,
       [server_name] nvarchar(1000),
       [db_instance] nvarchar(max),
       [db_name] nvarchar(max),
       [creation_time] datetime,
       [is_system] bit,
       [db_id] int,
       [compatibility] int,
       [family_guid] uniqueidentifier,
       [local_creation_time] datetime,
	   [local_creation_time_str] varchar(max),
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [obj_id], [server_name], [db_instance], [db_name], [creation_time], [is_system], [db_id], [compatibility], [family_guid], [local_creation_time], [local_creation_time_str], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [obj_id] uniqueidentifier 'obj_id',
       [server_name] nvarchar(1000) 'server_name',
       [db_instance] nvarchar(max) 'db_instance',
       [db_name] nvarchar(max) 'db_name',
       [creation_time] datetime 'creation_time',
       [is_system] bit 'is_system',
       [db_id] int 'db_id',
       [compatibility] int 'compatibility',
       [family_guid] uniqueidentifier 'family_guid',
       [local_creation_time] datetime 'local_creation_time',
	   [local_creation_time_str] varchar(max) 'local_creation_time_str',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
   	UPDATE @imported_changes
    SET
        [local_creation_time] = DATEADD( minute, -@TimeZoneShift, [local_creation_time])
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Backup.Model.GuestDatabase]
    SET
        [80.Backup.Model.GuestDatabase].[obj_id] = src.[obj_id],
        [80.Backup.Model.GuestDatabase].[server_name] = src.[server_name],
        [80.Backup.Model.GuestDatabase].[db_instance] = src.[db_instance],
        [80.Backup.Model.GuestDatabase].[db_name] = src.[db_name],
        [80.Backup.Model.GuestDatabase].[creation_time] = src.[creation_time],
        [80.Backup.Model.GuestDatabase].[is_system] = src.[is_system],
        [80.Backup.Model.GuestDatabase].[db_id] = src.[db_id],
        [80.Backup.Model.GuestDatabase].[compatibility] = src.[compatibility],
        [80.Backup.Model.GuestDatabase].[family_guid] = src.[family_guid],
        [80.Backup.Model.GuestDatabase].[local_creation_time] = src.[local_creation_time],
		[80.Backup.Model.GuestDatabase].[local_creation_time_str] = src.[local_creation_time_str],
        [80.Backup.Model.GuestDatabase].[usn] = src.[usn]
    FROM [80.Backup.Model.GuestDatabase] INNER JOIN @imported_changes src
    ON [80.Backup.Model.GuestDatabase].id = src.id    AND [80.Backup.Model.GuestDatabase].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.Backup.Model.GuestDatabase]([id], [obj_id], [server_name], [db_instance], [db_name], [creation_time], [is_system], [db_id], [compatibility], [family_guid], [local_creation_time], [local_creation_time_str], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[obj_id],
        src.[server_name],
        src.[db_instance],
        src.[db_name],
        src.[creation_time],
        src.[is_system],
        src.[db_id],
        src.[compatibility],
        src.[family_guid],
        src.[local_creation_time],
		src.[local_creation_time_str],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Backup.Model.GuestDatabase] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_GuestDatabase_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_GuestDatabase_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_GuestDatabase_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_GuestDatabase_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Backup.Model.GuestDatabase]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Backup.Model.GuestDatabase].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_WanAccelerators_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_WanAccelerators_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_WanAccelerators_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_WanAccelerators_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [host_id] uniqueidentifier,
       [name] nvarchar(max),
       [description] nvarchar(max),
       [is_unavailable] bit,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [host_id], [name], [description], [is_unavailable], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [host_id] uniqueidentifier 'host_id',
       [name] nvarchar(max) 'name',
       [description] nvarchar(max) 'description',
       [is_unavailable] bit 'is_unavailable',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.WanAccelerators]
    SET
        [80.WanAccelerators].[host_id] = src.[host_id],
        [80.WanAccelerators].[name] = src.[name],
        [80.WanAccelerators].[description] = src.[description],
        [80.WanAccelerators].[is_unavailable] = src.[is_unavailable],
        [80.WanAccelerators].[usn] = src.[usn]
    FROM [80.WanAccelerators] INNER JOIN @imported_changes src
    ON [80.WanAccelerators].id = src.id    AND [80.WanAccelerators].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.WanAccelerators]([id], [host_id], [name], [description], [is_unavailable], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[host_id],
        src.[name],
        src.[description],
        src.[is_unavailable],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.WanAccelerators] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_WanAccelerators_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_WanAccelerators_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_WanAccelerators_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_WanAccelerators_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.WanAccelerators]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.WanAccelerators].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_HostComponents_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_HostComponents_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_HostComponents_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_HostComponents_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [physical_host_id] uniqueidentifier,
       [type] int,
       [version] nvarchar(255),
       [options] xml,
       [is_up_to_date] bit,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [physical_host_id], [type], [version], [options], [is_up_to_date], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [physical_host_id] uniqueidentifier 'physical_host_id',
       [type] int 'type',
       [version] nvarchar(255) 'version',
       [options] xml 'options/*',
       [is_up_to_date] bit 'is_up_to_date',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.HostComponents]
    SET
        [80.HostComponents].[physical_host_id] = src.[physical_host_id],
        [80.HostComponents].[type] = src.[type],
        [80.HostComponents].[version] = src.[version],
        [80.HostComponents].[options] = src.[options],
        [80.HostComponents].[is_up_to_date] = src.[is_up_to_date],
        [80.HostComponents].[usn] = src.[usn]
    FROM [80.HostComponents] INNER JOIN @imported_changes src
    ON [80.HostComponents].id = src.id    AND [80.HostComponents].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.HostComponents]([id], [physical_host_id], [type], [version], [options], [is_up_to_date], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[physical_host_id],
        src.[type],
        src.[version],
        src.[options],
        src.[is_up_to_date],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.HostComponents] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_HostComponents_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_HostComponents_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_HostComponents_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_HostComponents_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.HostComponents]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.HostComponents].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_CloudGates_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_CloudGates_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_CloudGates_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_CloudGates_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [name] nvarchar(max),
       [description] nvarchar(max),
       [creds_id] uniqueidentifier,
       [options] xml,
       [host_id] uniqueidentifier,
       [disabled] bit,
       [is_available] bit,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [name], [description], [creds_id], [options], [host_id], [disabled], [is_available], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [name] nvarchar(max) 'name',
       [description] nvarchar(max) 'description',
       [creds_id] uniqueidentifier 'creds_id',
       [options] xml 'options/*',
       [host_id] uniqueidentifier 'host_id',
       [disabled] bit 'disabled',
       [is_available] bit 'is_available',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.Backup.Model.CloudGates]
    SET
        [80.Backup.Model.CloudGates].[name] = src.[name],
        [80.Backup.Model.CloudGates].[description] = src.[description],
        [80.Backup.Model.CloudGates].[creds_id] = src.[creds_id],
        [80.Backup.Model.CloudGates].[options] = src.[options],
        [80.Backup.Model.CloudGates].[host_id] = src.[host_id],
        [80.Backup.Model.CloudGates].[disabled] = src.[disabled],
        [80.Backup.Model.CloudGates].[is_available] = src.[is_available],
        [80.Backup.Model.CloudGates].[usn] = src.[usn]
    FROM [80.Backup.Model.CloudGates] INNER JOIN @imported_changes src
    ON [80.Backup.Model.CloudGates].id = src.id    AND [80.Backup.Model.CloudGates].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.Backup.Model.CloudGates]([id], [name], [description], [creds_id], [options], [host_id], [disabled], [is_available], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[name],
        src.[description],
        src.[creds_id],
        src.[options],
        src.[host_id],
        src.[disabled],
        src.[is_available],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.Backup.Model.CloudGates] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Backup_Model_CloudGates_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Backup_Model_CloudGates_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Backup_Model_CloudGates_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Backup_Model_CloudGates_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Backup.Model.CloudGates]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Backup.Model.CloudGates].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_Tenants_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Tenants_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_Tenants_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Tenants_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier,
       [name] nvarchar(max),
       [description] nvarchar(max),
       [expire_time] datetime,
       [disabled] bit,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [name], [description], [expire_time], [disabled], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [name] nvarchar(max) 'name',
       [description] nvarchar(max) 'description',
       [expire_time] datetime 'expire_time',
       [disabled] bit 'disabled',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

	MERGE [80.Tenants] tgt
	USING @imported_changes src ON tgt.id = src.id AND tgt.db_instance_id = @dbInstanceId
	WHEN MATCHED THEN
		UPDATE SET
			tgt.[name] = src.[name],
			tgt.[description] = src.[description],
			tgt.[expire_time] = src.[expire_time],
			tgt.[disabled] = src.[disabled],
			tgt.[usn] = src.[usn]
	WHEN NOT MATCHED BY TARGET THEN
		INSERT ([id], [name], [description], [expire_time], [disabled], [usn], db_instance_id)
		VALUES (
			src.[id],
			src.[name],
			src.[description],
			src.[expire_time],
			src.[disabled],
			src.[usn],
			@dbInstanceId
		)
	OUTPUT
		inserted.[id] AS [id],
		CASE $action WHEN 'INSERT' THEN 0 ELSE 1 END AS [change_type];
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_Tenants_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_Tenants_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_Tenants_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_Tenants_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.Tenants]
	OUTPUT deleted.[id]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.Tenants].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------


-----------------------------------------------------
--[dbo].[usp_Import_80_TenantsResourcesQuota_Changes]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_TenantsResourcesQuota_Changes]') AND type in (N'P'))
     DROP PROCEDURE [dbo].[usp_Import_80_TenantsResourcesQuota_Changes]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_TenantsResourcesQuota_Changes]
     @dbInstanceId uniqueidentifier,
     @timeZoneShift int,
     @importDoc XML
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @imported_changes table
    (
       [id] uniqueidentifier NOT NULL,
       [tenant_id] uniqueidentifier NOT NULL,
       [repository_id] uniqueidentifier NOT NULL,
       [folder] nvarchar(max),
       [wan_id] uniqueidentifier,
       [quota_mb] int,
       [used_quota_mb] int NOT NULL,
       [friendly_name] nvarchar(max) NOT NULL,
       [usn] bigint,
       [db_instance_id] uniqueidentifier
    )

    /*              IMPORT XML DOCUMENT      */
    DECLARE @idoc int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @importDoc

    INSERT INTO @imported_changes([id], [tenant_id], [repository_id], [folder], [wan_id], [quota_mb], [used_quota_mb], [friendly_name], [usn])
    SELECT *
    FROM OPENXML( @idoc, '/Root/row', 2)
    WITH
    (
       [id] uniqueidentifier 'id',
       [tenant_id] uniqueidentifier 'tenant_id',
       [repository_id] uniqueidentifier 'repository_id',
       [folder] nvarchar(max) 'folder',
       [wan_id] uniqueidentifier 'wan_id',
       [quota_mb] int 'quota_mb',
       [used_quota_mb] int 'used_quota_mb',
       [friendly_name] nvarchar(max) 'friendly_name',
       [usn] bigint 'usn'
    )
    EXEC sp_xml_removedocument @idoc


    /*             ADD DBID  FIELD          */
    UPDATE @imported_changes
    SET db_instance_id= @dbInstanceId

    /*             TIMEZONE UPDATES         */
    /*             UPDATE EXISTING ROWS     */
    UPDATE [80.TenantsResourcesQuota]
    SET
        [80.TenantsResourcesQuota].[tenant_id] = src.[tenant_id],
        [80.TenantsResourcesQuota].[repository_id] = src.[repository_id],
        [80.TenantsResourcesQuota].[folder] = src.[folder],
        [80.TenantsResourcesQuota].[wan_id] = src.[wan_id],
        [80.TenantsResourcesQuota].[quota_mb] = src.[quota_mb],
        [80.TenantsResourcesQuota].[used_quota_mb] = src.[used_quota_mb],
        [80.TenantsResourcesQuota].[friendly_name] = src.[friendly_name],
        [80.TenantsResourcesQuota].[usn] = src.[usn]
    FROM [80.TenantsResourcesQuota] INNER JOIN @imported_changes src
    ON [80.TenantsResourcesQuota].id = src.id    AND [80.TenantsResourcesQuota].db_instance_id = @dbInstanceId

    /*      INSERT NEW ROWS       */
    INSERT INTO [80.TenantsResourcesQuota]([id], [tenant_id], [repository_id], [folder], [wan_id], [quota_mb], [used_quota_mb], [friendly_name], [usn], db_instance_id)
    SELECT
        src.[id],
        src.[tenant_id],
        src.[repository_id],
        src.[folder],
        src.[wan_id],
        src.[quota_mb],
        src.[used_quota_mb],
        src.[friendly_name],
        src.[usn],
        src.db_instance_id
    FROM @imported_changes src LEFT OUTER JOIN [80.TenantsResourcesQuota] trg
    ON src.id = trg.id
    WHERE trg.id IS NULL AND src.db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------




-----------------------------------------------------
--[dbo].[usp_Import_80_TenantsResourcesQuota_Dels]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_Import_80_TenantsResourcesQuota_Dels]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_Import_80_TenantsResourcesQuota_Dels]
GO
CREATE PROCEDURE [dbo].[usp_Import_80_TenantsResourcesQuota_Dels]
    @deleted_objects_xml XML,
    @dbInstanceId uniqueidentifier
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @deleted_objects_table table
    (
       obj_id  uniqueidentifier
    )

    /* importing deleted objects set  */
    DECLARE @idoc  int
    EXEC sp_xml_preparedocument @idoc OUTPUT, @deleted_objects_xml
    INSERT INTO @deleted_objects_table( obj_id )
    SELECT *
    FROM OPENXML( @idoc, N'/root/object',1 )
    WITH
    (
       obj_id uniqueidentifier '@id'    )
    EXEC sp_xml_removedocument @idoc

    DELETE FROM [dbo].[80.TenantsResourcesQuota]
    WHERE id IN ( SELECT obj_id FROM @deleted_objects_table )
    AND [dbo].[80.TenantsResourcesQuota].db_instance_id = @dbInstanceId
END
GO
-----------------------------------------------------

