UFN_PDCOMPOSITESEGMENTMAPPINGENTRY_VALIDMAPPING

Returns 1 if the PDCOMPOSITESEGMENT record is unique, 0 otherwise if it is a duplicate of another record.

Return

Return Type
bit

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier IN
@PDCOMPOSITESEGMENTMAPPINGID uniqueidentifier IN

Definition

Copy


CREATE function dbo.UFN_PDCOMPOSITESEGMENTMAPPINGENTRY_VALIDMAPPING
(
@ID uniqueidentifier,
@PDCOMPOSITESEGMENTMAPPINGID uniqueidentifier
)
returns bit
with execute as caller
as 
begin
declare @Valid bit
declare @PDACCOUNTSYSTEMID uniqueidentifier 
select @PDACCOUNTSYSTEMID = b.PDACCOUNTSYSTEMID from PDCOMPOSITESEGMENTMAPPING as a join PDACCOUNTSTRUCTURE as b on a.PDACCOUNTSTRUCTUREID = b.ID where a.ID = @PDCOMPOSITESEGMENTMAPPINGID 
declare @NumTables tinyint
declare @NumTablesProcessed tinyint
declare @NumTablesMatched tinyint
    select @NumTables = count(distinct a.ID) from PDCOMPOSITESEGMENTTABLELIST as a  join PDCOMPOSITESEGMENTMAPPING as b on a.PDCOMPOSITESEGMENTID = b.PDCOMPOSITESEGMENTID  join PDCOMPOSITESEGMENTMAPPINGENTRY as c on c.PDCOMPOSITESEGMENTMAPPINGID = b.ID where b.ID = @PDCOMPOSITESEGMENTMAPPINGID 
    select @NumTablesProcessed = count(ID) from PDCOMPOSITESEGMENTMAPPINGENTRY where PDCOMPOSITESEGMENTMAPPINGID = @PDCOMPOSITESEGMENTMAPPINGID
    if @NumTablesProcessed = @NumTables
        begin
        declare @COMPOSITESEGMENTKEY nvarchar(144)
        set @COMPOSITESEGMENTKEY = dbo.UFN_PDCOMPOSITESEGMENTMAPPING_KEY(@PDCOMPOSITESEGMENTMAPPINGID)

        if exists (select * from PDCOMPOSITESEGMENTMAPPING 
             as a join PDACCOUNTSTRUCTURE as b on a.PDACCOUNTSTRUCTUREID = b.ID 
            where COMPOSITESEGMENTKEY = @COMPOSITESEGMENTKEY and a.ID != @PDCOMPOSITESEGMENTMAPPINGID and b.PDACCOUNTSYSTEMID = @PDACCOUNTSYSTEMID 
            and PDCOMPOSITESEGMENTID in (select PDCOMPOSITESEGMENTID from PDCOMPOSITESEGMENTMAPPING as a join PDACCOUNTSTRUCTURE as b on a.PDACCOUNTSTRUCTUREID = b.ID  where a.ID = @PDCOMPOSITESEGMENTMAPPINGID and b.PDACCOUNTSYSTEMID = @PDACCOUNTSYSTEMID ))                
            set @Valid = 0
        end     
    else
        set @Valid = 1

return @Valid
end