Stored Procedures [dbo].[SubscribeToConversation]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)
@inSysUserIdint4
@inOrderIdint4
@inContactPriKeyint4
Permissions
TypeActionOwning Principal
GrantExecuteMssExec
SQL Script
create procedure [dbo].[SubscribeToConversation]
    @inSysUserId int,
    @inOrderId int,
    @inContactPriKey int
as
begin
    set nocount on;
    ;with insertUpdateSakariContactSysuserMap as
    (
        select
        SysuserFID = @inSysUserId,
        SakariContactFID = @inContactPriKey,
        LastReadAt = sysdatetimeoffset(),
        Subscribed = 1
    )
    merge SakariContactSysuserMap with (tablock)
    using insertUpdateSakariContactSysuserMap on
        SakariContactSysuserMap.SysUserFID = insertUpdateSakariContactSysuserMap.SysuserFID and
        SakariContactSysuserMap.SakariContactFID = insertUpdateSakariContactSysuserMap.SakariContactFID
    when matched then
        update set
        LastReadAt = insertUpdateSakariContactSysuserMap.LastReadAt,
        Subscribed = insertUpdateSakariContactSysuserMap.Subscribed
    when not matched then
        insert
        (
            SysuserFID,
            SakariContactFID,
            LastReadAt,
            Subscribed
        )
        values
        (
            insertUpdateSakariContactSysuserMap.SysuserFID,
            insertUpdateSakariContactSysuserMap.SakariContactFID,
            insertUpdateSakariContactSysuserMap.LastReadAt,
            insertUpdateSakariContactSysuserMap.Subscribed
        );

    insert into SakariContactOrderMap
    (
        OrderFID,
        SakariContactFID
    )
    select
        @inOrderId,
        @inContactPriKey
    where not exists
    (
        select top 1 1
        from SakariContactOrderMap
        where
            OrderFID = @inOrderId and
            SakariContactFID = @inContactPriKey
    )
end
GO
GRANT EXECUTE ON  [dbo].[SubscribeToConversation] TO [MssExec]
GO
Uses