USP_BATCHADDRESSVALIDATION_DELETEBATCH

Executes the "Address Validation Batch: Delete" record operation.

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier IN Input parameter indicating the ID of the record being deleted.
@CHANGEAGENTID uniqueidentifier IN Input parameter indicating the ID of the change agent invoking the delete.
@CURRENTAPPUSERID uniqueidentifier IN Input parameter indicating the ID of the current user.

Definition

Copy


CREATE procedure dbo.USP_BATCHADDRESSVALIDATION_DELETEBATCH
(
    @ID uniqueidentifier,
    @CHANGEAGENTID uniqueidentifier,
    @CURRENTAPPUSERID uniqueidentifier = null
)
as
    set nocount on;

    begin try
        declare @CURRENTDATE datetime;
        set @CURRENTDATE = getdate();

        declare @CONTEXTCACHE varbinary(128);
        set @CONTEXTCACHE = CONTEXT_INFO();
        declare @LOCKNAME nvarchar(36);
        set @LOCKNAME = upper(cast(@ID as nvarchar(36)));
        declare @RESULT int;

        exec @RESULT = sp_getapplock @RESOURCE=@LOCKNAME, @LOCKMODE='Exclusive', @LOCKOWNER='Session', @LOCKTIMEOUT=0;

        if @RESULT = 0
        begin
            if @CHANGEAGENTID is null
                exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output;

            set CONTEXT_INFO @CHANGEAGENTID;

            if @CURRENTAPPUSERID is not null
            begin
                declare @GRANTED bit;
                select @GRANTED=dbo.UFN_SECURITY_APPUSER_GRANTED_BATCHOWNER(@CURRENTAPPUSERID, @ID);
                if @GRANTED = 0
                    raiserror('You do not have permission to delete this batch.', 13, 1);
            end;

            delete from dbo.BATCHADDRESSVALIDATION where BATCHID = @ID;

            exec sp_releaseapplock @Resource=@lockName, @LockOwner='Session';

            if not @contextCache is null
                set CONTEXT_INFO @contextCache
        end;
        else
            raiserror('This batch is in use and cannot be deleted.', 13, 1);
    end try
    begin catch
        exec dbo.USP_RAISE_ERROR;
        return 1;
    end catch

    return 0;