USP_EVENTPRICE_GETBENEFITS_UPDATEFROMXML

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

Parameters

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

Definition

Copy
/*
Generated by Blackbaud AppFx Platform
Date:  12/17/2008 2:12:59 AM
Assembly Version:  Blackbaud.AppFx.Platform.SqlClr, Version=1.7.1271.0, Culture=neutral, PublicKeyToken=null
Copyright Blackbaud
*/
CREATE PROCEDURE dbo.USP_EVENTPRICE_GETBENEFITS_UPDATEFROMXML 
(
@EVENTPRICEID 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_EVENTPRICE_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.[EVENTPRICEBENEFIT] where [EVENTPRICEBENEFIT].ID in 
    (select ID from dbo.UFN_EVENTPRICE_GETBENEFITS
    (
        @EVENTPRICEID
    )
    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 [EVENTPRICEBENEFIT]
        set [EVENTPRICEBENEFIT].[BENEFITID]=temp.[BENEFITID],
        [EVENTPRICEBENEFIT].[DETAILS]=temp.[DETAILS],
        [EVENTPRICEBENEFIT].[ID]=temp.[ID],
        [EVENTPRICEBENEFIT].[QUANTITY]=temp.[QUANTITY],
        [EVENTPRICEBENEFIT].[TOTALVALUE]=temp.[TOTALVALUE],
        [EVENTPRICEBENEFIT].[UNITVALUE]=temp.[UNITVALUE],
        [EVENTPRICEBENEFIT].CHANGEDBYID = @CHANGEAGENTID,
        [EVENTPRICEBENEFIT].DATECHANGED = @CHANGEDATE

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

if @@Error <> 0
    return 3;    

-- insert new items

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

if @@Error <> 0
    return 4;

return 0;