USP_PROSPECTPLAN_STEPS_2_UPDATEFROMXML

Used to update a set of records defined by UFN_PROSPECTPLAN_STEPS_2 from the given xml string.

Parameters

Parameter Parameter Type Mode Description
@PROSPECTPLANID uniqueidentifier IN
@XML xml IN
@CHANGEAGENTID uniqueidentifier IN
@CHANGEDATE datetime IN

Definition

Copy
/*
Generated by Blackbaud AppFx Platform
Date:  12/15/2016 8:40:54 PM
Assembly Version:  Blackbaud.AppFx.Platform.SqlClr, Version=4.0.165.0, Culture=neutral, PublicKeyToken=null
Copyright Blackbaud
*/
CREATE PROCEDURE dbo.USP_PROSPECTPLAN_STEPS_2_UPDATEFROMXML 
(
@PROSPECTPLANID uniqueidentifier,
@XML xml,
@CHANGEAGENTID uniqueidentifier = null,
@CHANGEDATE datetime = null
)

as

set nocount on;

if @CHANGEAGENTID is null
    exec USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output

if @CHANGEDATE is null 
    set @CHANGEDATE = getdate()

-- build a temporary table containing the values from the XML

declare @TempTbl table (
   [ACTUALDATE] datetime,
   [ACTUALENDTIME] varchar(4),
   [ACTUALSTARTTIME] varchar(4),
   [COMMENT] nvarchar(max),
   [CONSTITUENTID] uniqueidentifier,
   [EXPECTEDDATE] datetime,
   [EXPECTEDENDTIME] varchar(4),
   [EXPECTEDSTARTTIME] varchar(4),
   [FUNDRAISERID] uniqueidentifier,
   [ID] uniqueidentifier,
   [INTERACTIONSUBCATEGORYID] uniqueidentifier,
   [INTERACTIONTYPECODEID] uniqueidentifier,
   [ISALLDAYEVENT] bit,
   [LOCATION] nvarchar(300),
   [OBJECTIVE] nvarchar(100),
   [PLANOUTLINESTEPID] uniqueidentifier,
   [PROSPECTPLANSTATUSCODEID] uniqueidentifier,
   [STATUSCODE] tinyint,
   [TIMEZONEENTRYID] uniqueidentifier)

insert into @TempTbl select 
    [ACTUALDATE],
    [ACTUALENDTIME],
    [ACTUALSTARTTIME],
    [COMMENT],
    [CONSTITUENTID],
    [EXPECTEDDATE],
    [EXPECTEDENDTIME],
    [EXPECTEDSTARTTIME],
    [FUNDRAISERID],
    [ID],
    [INTERACTIONSUBCATEGORYID],
    [INTERACTIONTYPECODEID],
    [ISALLDAYEVENT],
    [LOCATION],
    [OBJECTIVE],
    [PLANOUTLINESTEPID],
    [PROSPECTPLANSTATUSCODEID],
    [STATUSCODE],
    [TIMEZONEENTRYID] 
from dbo.UFN_PROSPECTPLAN_STEPS_2_FROMITEMLISTXML(@XML)

update @TempTbl set ID = newid() where (ID is null) or (ID = '00000000-0000-0000-0000-000000000000');

if @@Error <> 0
    return 1;

declare @contextCache varbinary(128);
declare @e int;

-- cache current context information 

set @contextCache = CONTEXT_INFO();

-- set CONTEXT_INFO to @CHANGEAGENTID 

if not @CHANGEAGENTID is null
    set CONTEXT_INFO @CHANGEAGENTID;

-- delete any items that no longer exist in the XML table

delete from dbo.[INTERACTION] where [INTERACTION].ID in 
    (select ID from dbo.UFN_PROSPECTPLAN_STEPS_2
    (
        @PROSPECTPLANID
    )
    EXCEPT select ID from @TempTbl)    

select @e=@@error;

-- reset CONTEXT_INFO to previous value 

if not @contextCache is null
    set CONTEXT_INFO @contextCache;

if @e <> 0
    return 2;

-- update the items that exist in the XML table and the db

update [INTERACTION]
        set [INTERACTION].[ACTUALDATE]=temp.[ACTUALDATE],
        [INTERACTION].[ACTUALENDTIME]=temp.[ACTUALENDTIME],
        [INTERACTION].[ACTUALSTARTTIME]=temp.[ACTUALSTARTTIME],
        [INTERACTION].[COMMENT]=temp.[COMMENT],
        [INTERACTION].[CONSTITUENTID]=temp.[CONSTITUENTID],
        [INTERACTION].[EXPECTEDDATE]=temp.[EXPECTEDDATE],
        [INTERACTION].[EXPECTEDENDTIME]=temp.[EXPECTEDENDTIME],
        [INTERACTION].[EXPECTEDSTARTTIME]=temp.[EXPECTEDSTARTTIME],
        [INTERACTION].[FUNDRAISERID]=temp.[FUNDRAISERID],
        [INTERACTION].[ID]=temp.[ID],
        [INTERACTION].[INTERACTIONSUBCATEGORYID]=temp.[INTERACTIONSUBCATEGORYID],
        [INTERACTION].[INTERACTIONTYPECODEID]=temp.[INTERACTIONTYPECODEID],
        [INTERACTION].[ISALLDAYEVENT]=temp.[ISALLDAYEVENT],
        [INTERACTION].[LOCATION]=temp.[LOCATION],
        [INTERACTION].[OBJECTIVE]=temp.[OBJECTIVE],
        [INTERACTION].[PLANOUTLINESTEPID]=temp.[PLANOUTLINESTEPID],
        [INTERACTION].[PROSPECTPLANSTATUSCODEID]=temp.[PROSPECTPLANSTATUSCODEID],
        [INTERACTION].[STATUSCODE]=temp.[STATUSCODE],
        [INTERACTION].[TIMEZONEENTRYID]=temp.[TIMEZONEENTRYID],
        [INTERACTION].CHANGEDBYID = @CHANGEAGENTID,
        [INTERACTION].DATECHANGED = @CHANGEDATE

    from dbo.[INTERACTION] inner join @TempTbl as [temp] on [INTERACTION].ID = [temp].ID
    where ([INTERACTION].[ACTUALDATE]<>temp.[ACTUALDATE]) or 
        ([INTERACTION].[ACTUALDATE] is null and temp.[ACTUALDATE] is not null) or 
        ([INTERACTION].[ACTUALDATE] is not null and temp.[ACTUALDATE] is null) or 
        ([INTERACTION].[ACTUALENDTIME]<>temp.[ACTUALENDTIME]) or 
        ([INTERACTION].[ACTUALENDTIME] is null and temp.[ACTUALENDTIME] is not null) or 
        ([INTERACTION].[ACTUALENDTIME] is not null and temp.[ACTUALENDTIME] is null) or 
        ([INTERACTION].[ACTUALSTARTTIME]<>temp.[ACTUALSTARTTIME]) or 
        ([INTERACTION].[ACTUALSTARTTIME] is null and temp.[ACTUALSTARTTIME] is not null) or 
        ([INTERACTION].[ACTUALSTARTTIME] is not null and temp.[ACTUALSTARTTIME] is null) or 
        ([INTERACTION].[COMMENT]<>temp.[COMMENT]) or 
        ([INTERACTION].[COMMENT] is null and temp.[COMMENT] is not null) or 
        ([INTERACTION].[COMMENT] is not null and temp.[COMMENT] is null) or 
        ([INTERACTION].[CONSTITUENTID]<>temp.[CONSTITUENTID]) or 
        ([INTERACTION].[CONSTITUENTID] is null and temp.[CONSTITUENTID] is not null) or 
        ([INTERACTION].[CONSTITUENTID] is not null and temp.[CONSTITUENTID] is null) or 
        ([INTERACTION].[EXPECTEDDATE]<>temp.[EXPECTEDDATE]) or 
        ([INTERACTION].[EXPECTEDDATE] is null and temp.[EXPECTEDDATE] is not null) or 
        ([INTERACTION].[EXPECTEDDATE] is not null and temp.[EXPECTEDDATE] is null) or 
        ([INTERACTION].[EXPECTEDENDTIME]<>temp.[EXPECTEDENDTIME]) or 
        ([INTERACTION].[EXPECTEDENDTIME] is null and temp.[EXPECTEDENDTIME] is not null) or 
        ([INTERACTION].[EXPECTEDENDTIME] is not null and temp.[EXPECTEDENDTIME] is null) or 
        ([INTERACTION].[EXPECTEDSTARTTIME]<>temp.[EXPECTEDSTARTTIME]) or 
        ([INTERACTION].[EXPECTEDSTARTTIME] is null and temp.[EXPECTEDSTARTTIME] is not null) or 
        ([INTERACTION].[EXPECTEDSTARTTIME] is not null and temp.[EXPECTEDSTARTTIME] is null) or 
        ([INTERACTION].[FUNDRAISERID]<>temp.[FUNDRAISERID]) or 
        ([INTERACTION].[FUNDRAISERID] is null and temp.[FUNDRAISERID] is not null) or 
        ([INTERACTION].[FUNDRAISERID] is not null and temp.[FUNDRAISERID] is null) or 
        ([INTERACTION].[ID]<>temp.[ID]) or 
        ([INTERACTION].[ID] is null and temp.[ID] is not null) or 
        ([INTERACTION].[ID] is not null and temp.[ID] is null) or 
        ([INTERACTION].[INTERACTIONSUBCATEGORYID]<>temp.[INTERACTIONSUBCATEGORYID]) or 
        ([INTERACTION].[INTERACTIONSUBCATEGORYID] is null and temp.[INTERACTIONSUBCATEGORYID] is not null) or 
        ([INTERACTION].[INTERACTIONSUBCATEGORYID] is not null and temp.[INTERACTIONSUBCATEGORYID] is null) or 
        ([INTERACTION].[INTERACTIONTYPECODEID]<>temp.[INTERACTIONTYPECODEID]) or 
        ([INTERACTION].[INTERACTIONTYPECODEID] is null and temp.[INTERACTIONTYPECODEID] is not null) or 
        ([INTERACTION].[INTERACTIONTYPECODEID] is not null and temp.[INTERACTIONTYPECODEID] is null) or 
        ([INTERACTION].[ISALLDAYEVENT]<>temp.[ISALLDAYEVENT]) or 
        ([INTERACTION].[ISALLDAYEVENT] is null and temp.[ISALLDAYEVENT] is not null) or 
        ([INTERACTION].[ISALLDAYEVENT] is not null and temp.[ISALLDAYEVENT] is null) or 
        ([INTERACTION].[LOCATION]<>temp.[LOCATION]) or 
        ([INTERACTION].[LOCATION] is null and temp.[LOCATION] is not null) or 
        ([INTERACTION].[LOCATION] is not null and temp.[LOCATION] is null) or 
        ([INTERACTION].[OBJECTIVE]<>temp.[OBJECTIVE]) or 
        ([INTERACTION].[OBJECTIVE] is null and temp.[OBJECTIVE] is not null) or 
        ([INTERACTION].[OBJECTIVE] is not null and temp.[OBJECTIVE] is null) or 
        ([INTERACTION].[PLANOUTLINESTEPID]<>temp.[PLANOUTLINESTEPID]) or 
        ([INTERACTION].[PLANOUTLINESTEPID] is null and temp.[PLANOUTLINESTEPID] is not null) or 
        ([INTERACTION].[PLANOUTLINESTEPID] is not null and temp.[PLANOUTLINESTEPID] is null) or 
        ([INTERACTION].[PROSPECTPLANSTATUSCODEID]<>temp.[PROSPECTPLANSTATUSCODEID]) or 
        ([INTERACTION].[PROSPECTPLANSTATUSCODEID] is null and temp.[PROSPECTPLANSTATUSCODEID] is not null) or 
        ([INTERACTION].[PROSPECTPLANSTATUSCODEID] is not null and temp.[PROSPECTPLANSTATUSCODEID] is null) or 
        ([INTERACTION].[STATUSCODE]<>temp.[STATUSCODE]) or 
        ([INTERACTION].[STATUSCODE] is null and temp.[STATUSCODE] is not null) or 
        ([INTERACTION].[STATUSCODE] is not null and temp.[STATUSCODE] is null) or 
        ([INTERACTION].[TIMEZONEENTRYID]<>temp.[TIMEZONEENTRYID]) or 
        ([INTERACTION].[TIMEZONEENTRYID] is null and temp.[TIMEZONEENTRYID] is not null) or 
        ([INTERACTION].[TIMEZONEENTRYID] is not null and temp.[TIMEZONEENTRYID] is null)

if @@Error <> 0
    return 3;    

-- insert new items

insert into [INTERACTION] 
    ([PROSPECTPLANID], 
    [ACTUALDATE],
    [ACTUALENDTIME],
    [ACTUALSTARTTIME],
    [COMMENT],
    [CONSTITUENTID],
    [EXPECTEDDATE],
    [EXPECTEDENDTIME],
    [EXPECTEDSTARTTIME],
    [FUNDRAISERID],
    [ID],
    [INTERACTIONSUBCATEGORYID],
    [INTERACTIONTYPECODEID],
    [ISALLDAYEVENT],
    [LOCATION],
    [OBJECTIVE],
    [PLANOUTLINESTEPID],
    [PROSPECTPLANSTATUSCODEID],
    [STATUSCODE],
    [TIMEZONEENTRYID],                
    ADDEDBYID, 
    CHANGEDBYID, 
    DATEADDED, 
    DATECHANGED)
select @PROSPECTPLANID
    [ACTUALDATE],
    [ACTUALENDTIME],
    [ACTUALSTARTTIME],
    [COMMENT],
    [CONSTITUENTID],
    [EXPECTEDDATE],
    [EXPECTEDENDTIME],
    [EXPECTEDSTARTTIME],
    [FUNDRAISERID],
    [ID],
    [INTERACTIONSUBCATEGORYID],
    [INTERACTIONTYPECODEID],
    [ISALLDAYEVENT],
    [LOCATION],
    [OBJECTIVE],
    [PLANOUTLINESTEPID],
    [PROSPECTPLANSTATUSCODEID],
    [STATUSCODE],
    [TIMEZONEENTRYID], 
    @CHANGEAGENTID
    @CHANGEAGENTID
    @CHANGEDATE
    @CHANGEDATE
from @TempTbl as [temp]
where not exists (select ID from dbo.[INTERACTION] as data where data.ID = [temp].ID)

if @@Error <> 0
    return 4;

return 0;