Stored Procedures [dbo].[MssWebGetSakariContactByPhoneNumbers]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)
@inSysUserIdint4
@inOrderIdint4
@inProgrammaticPhoneNumbersNVarChar32Listmax
Permissions
TypeActionOwning Principal
GrantExecuteMssExec
SQL Script
create procedure [dbo].[MssWebGetSakariContactByPhoneNumbers]
(
    @inSysUserId int,
    @inOrderId int,
    @inProgrammaticPhoneNumbers NVarChar32List readonly
)
as
begin
    
;with OrderSakariAccount as (
    select top 1 SakariAccountBranchMap.SakariAccountFID
    from SakariAccountBranchMap
    join Orders on Orders.BranchPriKey = SakariAccountBranchMap.BranchFID
    where Orders.PriKey = @inOrderId
    ),
    orderContacts as (
        select
        ProgrammaticPhone = SakariContact.PhoneNumber,
        ContactPriKey = SakariContact.SakariContactID,
        SakariContact.ContactName,
        ContactIsMappedToUser = convert(bit, case when SakariContactSysUserMap.SakariContactSysuserMapID is null then 0 else 1 end),
        ContactIsMappedToOrder = convert(bit,1 )
        from @inProgrammaticPhoneNumbers inPhoneNumbers
        inner join SakariContact on inPhoneNumbers.Item =  SakariContact.PhoneNumber
        inner join  SakariContactOrderMap on SakariContactOrderMap.SakariContactFID = SakariContact.SakariContactID
        left outer join SakariContactSysUserMap on SakariContact.SakariContactID = SakariContactSysuserMap.SakariContactFID and
        SakariContactSysuserMap.SysuserFID = @inSysUserId
        where SakariContactOrderMap.OrderFID =  @inOrderId
    ),
    orderSakariContacts as (
        select
        ProgrammaticPhone = SakariContact.PhoneNumber,
        ContactPriKey = SakariContact.SakariContactID,
        SakariContact.ContactName,
        ContactIsMappedToUser = convert(bit, case when SakariContactSysUserMap.SakariContactSysuserMapID is null then 0 else 1 end)
        from @inProgrammaticPhoneNumbers inPhoneNumbers
        inner join SakariContact on
            inPhoneNumbers.Item = SakariContact.PhoneNumber
        inner join OrderSakariAccount on OrderSakariAccount.SakariAccountFID = SakariContact.SakariAccountFID
        left outer join SakariContactSysUserMap on
            SakariContact.SakariContactID = SakariContactSysuserMap.SakariContactFID and
            SakariContactSysuserMap.SysuserFID = @inSysUserId
    )
    select
    ProgrammaticPhone = inPhoneNumbers.Item,
    ContactPriKey = case when orderContacts.ContactPriKey is not null then orderContacts.ContactPriKey else orderSakariContacts.ContactPriKey end,
    ContactName = case when orderContacts.ContactName is not null then orderContacts.ContactName else orderSakariContacts.ContactName end,
    ContactIsMappedToUser= case when orderContacts.ContactIsMappedToUser is not null then orderContacts.ContactIsMappedToUser else orderSakariContacts.ContactIsMappedToUser end,
    ContactIsMappedToOrder = orderContacts.ContactIsMappedToOrder
    from @inProgrammaticPhoneNumbers inPhoneNumbers
    left join orderContacts on orderContacts.ProgrammaticPhone = inPhoneNumbers.Item
    left join orderSakariContacts on orderSakariContacts.ProgrammaticPhone = inPhoneNumbers.Item
    where orderContacts.ContactPriKey is not null or  orderSakariContacts.ContactPriKey is not null
end
GO
GRANT EXECUTE ON  [dbo].[MssWebGetSakariContactByPhoneNumbers] TO [MssExec]
GO
Uses