USP_BATCH_DOTOTALSMATCH

Returns true if a batch's projected total amount and count match its actual amount and count.

Parameters

Parameter Parameter Type Mode Description
@BATCHID uniqueidentifier IN
@TOTALSMATCH bit INOUT

Definition

Copy


            CREATE procedure dbo.USP_BATCH_DOTOTALSMATCH
            (
                @BATCHID uniqueidentifier,
                @TOTALSMATCH bit output
            )
            with execute as owner 
            as begin

                set @TOTALSMATCH = 0

                declare @AMOUNTCOLUMN nvarchar(100)
                declare @BASETABLENAME nvarchar(128)
                select
                    @AMOUNTCOLUMN=BATCHTYPECATALOG.AMOUNTCOLUMN,
                    @BASETABLENAME=BATCHTYPECATALOG.BASETABLENAME
                from dbo.BATCH
                    inner join dbo.BATCHTEMPLATE on BATCHTEMPLATE.ID=BATCH.BATCHTEMPLATEID
                    inner join dbo.BATCHTYPECATALOG on BATCHTYPECATALOG.ID=BATCHTEMPLATE.BATCHTYPECATALOGID
                where BATCH.ID=@BATCHID;

                -- Make sure the batch table actually has the amount column and base table set.

                -- If they don't, the batch doesn't support projected amounts and so the true

                -- will always be returned.

                if coalesce(@AMOUNTCOLUMN, '') = '' or coalesce(@BASETABLENAME, '') = ''
                    set @TOTALSMATCH = 1
                else
                begin
                    -- Verify that the amount column and base table are valid

                    if exists (    select 1 from INFORMATION_SCHEMA.COLUMNS
                                where
                                    TABLE_SCHEMA = 'dbo' and
                                    TABLE_NAME = @BASETABLENAME and
                                    COLUMN_NAME = @AMOUNTCOLUMN)
                    begin
                        declare @SQLTOEXEC nvarchar(1000)
                        set @SQLTOEXEC = '
                                declare @PROJECTEDAMOUNT money
                                declare @PROJECTEDCOUNT int
                                declare @ACTUALAMOUNT money
                                declare @ACTUALCOUNT int

                                select
                                    @PROJECTEDAMOUNT=PROJECTEDTOTALAMOUNT,
                                    @PROJECTEDCOUNT=PROJECTEDNUMBEROFRECORDS
                                from dbo.BATCH
                                where ID=@BATCHID;

                                select 
                                    @ACTUALAMOUNT = coalesce(sum(' + @AMOUNTCOLUMN + '),0),
                                    @ACTUALCOUNT = count(ID)
                                from dbo.' + @BASETABLENAME + '
                                where BATCHID = @BATCHID;

                                if (@PROJECTEDAMOUNT=@ACTUALAMOUNT and @PROJECTEDCOUNT=@ACTUALCOUNT)
                                begin
                                    select @TOTALSMATCH=1;
                                end
                                else
                                begin
                                    select @TOTALSMATCH=0;
                                end
                        '

                        EXEC sp_executesql @SQLTOEXEC,
                            N'@BATCHID uniqueidentifier,@TOTALSMATCH bit output'
                            @BATCHID,@TOTALSMATCH output
                    end
                end

                return 0
            end