USP_PRINTERLIST_EDIT_UPDATEFROMXML

Updates printers on a printer list from an xml field

Parameters

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

Definition

Copy


CREATE procedure [dbo].[USP_PRINTERLIST_EDIT_UPDATEFROMXML] 
(
@PRINTERLISTID 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 (
   [ID] uniqueidentifier,
   [PRINTERNAME] nvarchar(255))

insert into @TempTbl select 
    [ID],
    [PRINTERNAME] 
from dbo.UFN_PRINTERLIST_GETPRINTERS_FROMITEMLISTXML(@XML)

-- ADDENDUM: Ensure any printers no longer in the system are removed

delete from @TempTbl where PRINTERNAME not in (
    select distinct PRINTERNAME from dbo.WORKSTATION
)

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.[PRINTERLISTITEM] where ID not in (select ID from @TempTbl)    and PRINTERLISTID = @PRINTERLISTID

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 [PRINTERLISTITEM]
        set [PRINTERLISTITEM].[ID]=temp.[ID],
        [PRINTERLISTITEM].[PRINTERNAME]=temp.[PRINTERNAME],
        [PRINTERLISTITEM].CHANGEDBYID = @CHANGEAGENTID,
        [PRINTERLISTITEM].DATECHANGED = @CHANGEDATE

    from dbo.[PRINTERLISTITEM] inner join @TempTbl as [temp] on [PRINTERLISTITEM].ID = [temp].ID
    where ([PRINTERLISTITEM].[ID]<>temp.[ID]) or 
        ([PRINTERLISTITEM].[ID] is null and temp.[ID] is not null) or 
        ([PRINTERLISTITEM].[ID] is not null and temp.[ID] is null) or 
        ([PRINTERLISTITEM].[PRINTERNAME]<>temp.[PRINTERNAME]) or 
        ([PRINTERLISTITEM].[PRINTERNAME] is null and temp.[PRINTERNAME] is not null) or 
        ([PRINTERLISTITEM].[PRINTERNAME] is not null and temp.[PRINTERNAME] is null)

if @@Error <> 0
    return 3;    

-- insert new items

insert into [PRINTERLISTITEM] 
    ([PRINTERLISTID], 
    [ID],
    [PRINTERNAME],                
    ADDEDBYID, 
    CHANGEDBYID, 
    DATEADDED, 
    DATECHANGED)
select @PRINTERLISTID
    [ID],
    [PRINTERNAME], 
    @CHANGEAGENTID
    @CHANGEAGENTID
    @CHANGEDATE
    @CHANGEDATE
from @TempTbl as [temp]
where not exists (select ID from dbo.[PRINTERLISTITEM] as data where data.ID = [temp].ID)

if @@Error <> 0
    return 4;

return 0;