USP_REGISTRANT_GETBENEFITS_UPDATEFROMXML

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

Parameters

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

Definition

Copy
/*
Generated by Blackbaud AppFx Platform
Date:  12/17/2008 2:13:21 AM
Assembly Version:  Blackbaud.AppFx.Platform.SqlClr, Version=1.7.1271.0, Culture=neutral, PublicKeyToken=null
Copyright Blackbaud
*/
CREATE PROCEDURE dbo.USP_REGISTRANT_GETBENEFITS_UPDATEFROMXML 
(
@REGISTRANTID 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 (
   [BENEFITID] uniqueidentifier,
   [DETAILS] nvarchar(255),
   [ID] uniqueidentifier,
   [QUANTITY] int,
   [TOTALVALUE] money,
   [UNITVALUE] money)

insert into @TempTbl select 
    [BENEFITID],
    [DETAILS],
    [ID],
    [QUANTITY],
    [TOTALVALUE],
    [UNITVALUE] 
from dbo.UFN_REGISTRANT_GETBENEFITS_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.[REGISTRANTBENEFIT] where [REGISTRANTBENEFIT].ID in 
    (select ID from dbo.UFN_REGISTRANT_GETBENEFITS
    (
        @REGISTRANTID
    )
    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 [REGISTRANTBENEFIT]
        set [REGISTRANTBENEFIT].[BENEFITID]=temp.[BENEFITID],
        [REGISTRANTBENEFIT].[DETAILS]=temp.[DETAILS],
        [REGISTRANTBENEFIT].[ID]=temp.[ID],
        [REGISTRANTBENEFIT].[QUANTITY]=temp.[QUANTITY],
        [REGISTRANTBENEFIT].[TOTALVALUE]=temp.[TOTALVALUE],
        [REGISTRANTBENEFIT].[UNITVALUE]=temp.[UNITVALUE],
        [REGISTRANTBENEFIT].CHANGEDBYID = @CHANGEAGENTID,
        [REGISTRANTBENEFIT].DATECHANGED = @CHANGEDATE

    from dbo.[REGISTRANTBENEFIT] inner join @TempTbl as [temp] on [REGISTRANTBENEFIT].ID = [temp].ID
    where ([REGISTRANTBENEFIT].[BENEFITID]<>temp.[BENEFITID]) or 
        ([REGISTRANTBENEFIT].[BENEFITID] is null and temp.[BENEFITID] is not null) or 
        ([REGISTRANTBENEFIT].[BENEFITID] is not null and temp.[BENEFITID] is null) or 
        ([REGISTRANTBENEFIT].[DETAILS]<>temp.[DETAILS]) or 
        ([REGISTRANTBENEFIT].[DETAILS] is null and temp.[DETAILS] is not null) or 
        ([REGISTRANTBENEFIT].[DETAILS] is not null and temp.[DETAILS] is null) or 
        ([REGISTRANTBENEFIT].[ID]<>temp.[ID]) or 
        ([REGISTRANTBENEFIT].[ID] is null and temp.[ID] is not null) or 
        ([REGISTRANTBENEFIT].[ID] is not null and temp.[ID] is null) or 
        ([REGISTRANTBENEFIT].[QUANTITY]<>temp.[QUANTITY]) or 
        ([REGISTRANTBENEFIT].[QUANTITY] is null and temp.[QUANTITY] is not null) or 
        ([REGISTRANTBENEFIT].[QUANTITY] is not null and temp.[QUANTITY] is null) or 
        ([REGISTRANTBENEFIT].[TOTALVALUE]<>temp.[TOTALVALUE]) or 
        ([REGISTRANTBENEFIT].[TOTALVALUE] is null and temp.[TOTALVALUE] is not null) or 
        ([REGISTRANTBENEFIT].[TOTALVALUE] is not null and temp.[TOTALVALUE] is null) or 
        ([REGISTRANTBENEFIT].[UNITVALUE]<>temp.[UNITVALUE]) or 
        ([REGISTRANTBENEFIT].[UNITVALUE] is null and temp.[UNITVALUE] is not null) or 
        ([REGISTRANTBENEFIT].[UNITVALUE] is not null and temp.[UNITVALUE] is null)

if @@Error <> 0
    return 3;    

-- insert new items

insert into [REGISTRANTBENEFIT] 
    ([REGISTRANTID], 
    [BENEFITID],
    [DETAILS],
    [ID],
    [QUANTITY],
    [TOTALVALUE],
   [UNITVALUE],                
    ADDEDBYID, 
    CHANGEDBYID, 
    DATEADDED, 
    DATECHANGED)
select @REGISTRANTID
    [BENEFITID],
    [DETAILS],
    [ID],
    [QUANTITY],
    [TOTALVALUE],
    [UNITVALUE], 
    @CHANGEAGENTID
    @CHANGEAGENTID
    @CHANGEDATE
    @CHANGEDATE
from @TempTbl as [temp]
where not exists (select ID from dbo.[REGISTRANTBENEFIT] as data where data.ID = [temp].ID)

if @@Error <> 0
    return 4;

return 0;