USP_VOLUNTEER_GETAVAILABILITIES_UPDATEFROMXML

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

Parameters

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

Definition

Copy
/*
Generated by Blackbaud AppFx Platform
Date:  3/19/2013 1:42:41 AM
Assembly Version:  Blackbaud.AppFx.Platform.SqlClr, Version=3.0.504.0, Culture=neutral, PublicKeyToken=null
Copyright Blackbaud
*/
CREATE PROCEDURE dbo.USP_VOLUNTEER_GETAVAILABILITIES_UPDATEFROMXML 
(
@VOLUNTEERID 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 (
   [DAYOFWEEKCODE] tinyint,
   [ENDDATE] char(4),
   [ENDTIME] char(4),
   [FULLENDDATE] date,
   [FULLSTARTDATE] date,
   [ID] uniqueidentifier,
   [STARTDATE] char(4),
   [STARTTIME] char(4))

insert into @TempTbl select 
    [DAYOFWEEKCODE],
    [ENDDATE],
    [ENDTIME],
    [FULLENDDATE],
    [FULLSTARTDATE],
    [ID],
    [STARTDATE],
    [STARTTIME] 
from dbo.UFN_VOLUNTEER_GETAVAILABILITIES_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.[VOLUNTEERAVAILABILITY] where [VOLUNTEERAVAILABILITY].ID in 
    (select ID from dbo.UFN_VOLUNTEER_GETAVAILABILITIES
    (
        @VOLUNTEERID
    )
    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 [VOLUNTEERAVAILABILITY]
        set [VOLUNTEERAVAILABILITY].[DAYOFWEEKCODE]=temp.[DAYOFWEEKCODE],
        [VOLUNTEERAVAILABILITY].[ENDDATE]=temp.[ENDDATE],
        [VOLUNTEERAVAILABILITY].[ENDTIME]=temp.[ENDTIME],
        [VOLUNTEERAVAILABILITY].[FULLENDDATE]=temp.[FULLENDDATE],
        [VOLUNTEERAVAILABILITY].[FULLSTARTDATE]=temp.[FULLSTARTDATE],
        [VOLUNTEERAVAILABILITY].[ID]=temp.[ID],
        [VOLUNTEERAVAILABILITY].[STARTDATE]=temp.[STARTDATE],
        [VOLUNTEERAVAILABILITY].[STARTTIME]=temp.[STARTTIME],
        [VOLUNTEERAVAILABILITY].CHANGEDBYID = @CHANGEAGENTID,
        [VOLUNTEERAVAILABILITY].DATECHANGED = @CHANGEDATE

    from dbo.[VOLUNTEERAVAILABILITY] inner join @TempTbl as [temp] on [VOLUNTEERAVAILABILITY].ID = [temp].ID
    where ([VOLUNTEERAVAILABILITY].[DAYOFWEEKCODE]<>temp.[DAYOFWEEKCODE]) or 
        ([VOLUNTEERAVAILABILITY].[DAYOFWEEKCODE] is null and temp.[DAYOFWEEKCODE] is not null) or 
        ([VOLUNTEERAVAILABILITY].[DAYOFWEEKCODE] is not null and temp.[DAYOFWEEKCODE] is null) or 
        ([VOLUNTEERAVAILABILITY].[ENDDATE]<>temp.[ENDDATE]) or 
        ([VOLUNTEERAVAILABILITY].[ENDDATE] is null and temp.[ENDDATE] is not null) or 
        ([VOLUNTEERAVAILABILITY].[ENDDATE] is not null and temp.[ENDDATE] is null) or 
        ([VOLUNTEERAVAILABILITY].[ENDTIME]<>temp.[ENDTIME]) or 
        ([VOLUNTEERAVAILABILITY].[ENDTIME] is null and temp.[ENDTIME] is not null) or 
        ([VOLUNTEERAVAILABILITY].[ENDTIME] is not null and temp.[ENDTIME] is null) or 
        ([VOLUNTEERAVAILABILITY].[FULLENDDATE]<>temp.[FULLENDDATE]) or 
        ([VOLUNTEERAVAILABILITY].[FULLENDDATE] is null and temp.[FULLENDDATE] is not null) or 
        ([VOLUNTEERAVAILABILITY].[FULLENDDATE] is not null and temp.[FULLENDDATE] is null) or 
        ([VOLUNTEERAVAILABILITY].[FULLSTARTDATE]<>temp.[FULLSTARTDATE]) or 
        ([VOLUNTEERAVAILABILITY].[FULLSTARTDATE] is null and temp.[FULLSTARTDATE] is not null) or 
        ([VOLUNTEERAVAILABILITY].[FULLSTARTDATE] is not null and temp.[FULLSTARTDATE] is null) or 
        ([VOLUNTEERAVAILABILITY].[ID]<>temp.[ID]) or 
        ([VOLUNTEERAVAILABILITY].[ID] is null and temp.[ID] is not null) or 
        ([VOLUNTEERAVAILABILITY].[ID] is not null and temp.[ID] is null) or 
        ([VOLUNTEERAVAILABILITY].[STARTDATE]<>temp.[STARTDATE]) or 
        ([VOLUNTEERAVAILABILITY].[STARTDATE] is null and temp.[STARTDATE] is not null) or 
        ([VOLUNTEERAVAILABILITY].[STARTDATE] is not null and temp.[STARTDATE] is null) or 
        ([VOLUNTEERAVAILABILITY].[STARTTIME]<>temp.[STARTTIME]) or 
        ([VOLUNTEERAVAILABILITY].[STARTTIME] is null and temp.[STARTTIME] is not null) or 
        ([VOLUNTEERAVAILABILITY].[STARTTIME] is not null and temp.[STARTTIME] is null)

if @@Error <> 0
    return 3;    

-- insert new items

insert into [VOLUNTEERAVAILABILITY] 
    ([VOLUNTEERID], 
    [DAYOFWEEKCODE],
    [ENDDATE],
    [ENDTIME],
    [FULLENDDATE],
    [FULLSTARTDATE],
    [ID],
    [STARTDATE],
    [STARTTIME],                
    ADDEDBYID, 
    CHANGEDBYID, 
    DATEADDED, 
    DATECHANGED)
select @VOLUNTEERID
    [DAYOFWEEKCODE],
    [ENDDATE],
    [ENDTIME],
    [FULLENDDATE],
    [FULLSTARTDATE],
    [ID],
    [STARTDATE],
    [STARTTIME], 
    @CHANGEAGENTID
    @CHANGEAGENTID
    @CHANGEDATE
    @CHANGEDATE
from @TempTbl as [temp]
where not exists (select ID from dbo.[VOLUNTEERAVAILABILITY] as data where data.ID = [temp].ID)

if @@Error <> 0
    return 4;

return 0;