USP_SOLICITCODE_UPDATEFROMXML

Bulk updates solicit code values.

Parameters

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

Definition

Copy


create procedure dbo.USP_SOLICITCODE_UPDATEFROMXML 
(
    @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 (
   DESCRIPTION nvarchar(100),
   SITEID uniqueidentifier null,
   EXCLUSIONCODE tinyint,
   ID uniqueidentifier,
   SEQUENCE int)

insert into @TempTbl select 
    DESCRIPTION,
    SITEID,
    EXCLUSIONCODE,
    ID,
    SEQUENCE 
from 
    dbo.UFN_SOLICITCODE_GETSOLICITCODES_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.SOLICITCODE where SOLICITCODE.ID in 
    (select 
        ID 
    from 
        dbo.UFN_SOLICITCODE_GETSOLICITCODES()
    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 
    SOLICITCODE
set 
    SOLICITCODE.DESCRIPTION=temp.DESCRIPTION,
    SOLICITCODE.SITEID = temp.SITEID,
    SOLICITCODE.EXCLUSIONCODE=temp.EXCLUSIONCODE,
    SOLICITCODE.ID=temp.ID,
    SOLICITCODE.SEQUENCE=temp.SEQUENCE,
    SOLICITCODE.CHANGEDBYID = @CHANGEAGENTID,
    SOLICITCODE.DATECHANGED = @CHANGEDATE
from 
    dbo.SOLICITCODE 
    inner join @TempTbl as temp on SOLICITCODE.ID = temp.ID
where 
    (SOLICITCODE.DESCRIPTION<>temp.DESCRIPTION) or 
    (SOLICITCODE.DESCRIPTION is null and temp.DESCRIPTION is not null) or 
    (SOLICITCODE.DESCRIPTION is not null and temp.DESCRIPTION is null) or 
    (SOLICITCODE.SITEID<>temp.SITEID) or 
    (SOLICITCODE.SITEID is null and temp.SITEID is not null) or 
    (SOLICITCODE.SITEID is not null and temp.SITEID is null) or 
    (SOLICITCODE.EXCLUSIONCODE<>temp.EXCLUSIONCODE) or 
    (SOLICITCODE.EXCLUSIONCODE is null and temp.EXCLUSIONCODE is not null) or 
    (SOLICITCODE.EXCLUSIONCODE is not null and temp.EXCLUSIONCODE is null) or 
    (SOLICITCODE.ID<>temp.ID) or 
    (SOLICITCODE.ID is null and temp.ID is not null) or 
    (SOLICITCODE.ID is not null and temp.ID is null) or 
    (SOLICITCODE.SEQUENCE<>temp.SEQUENCE) or 
    (SOLICITCODE.SEQUENCE is null and temp.SEQUENCE is not null) or 
    (SOLICITCODE.SEQUENCE is not null and temp.SEQUENCE is null)

if @@Error <> 0
    return 3;    

-- insert new items

insert into SOLICITCODE 
    (SITEID, 
    DESCRIPTION,
    EXCLUSIONCODE,
    ID,
    SEQUENCE,                
    ADDEDBYID, 
    CHANGEDBYID, 
    DATEADDED, 
    DATECHANGED)
select 
    SITEID, 
    DESCRIPTION,
    EXCLUSIONCODE,
    ID,
    SEQUENCE
    @CHANGEAGENTID
    @CHANGEAGENTID
    @CHANGEDATE
    @CHANGEDATE
from 
    @TempTbl as temp
where 
    not exists (select ID from dbo.SOLICITCODE as data where data.ID = temp.ID)

if @@Error <> 0
    return 4;

return 0;