Stored Procedures [dbo].[BcImportAccounts]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)
@itemsBcAccountImportItemsmax
Permissions
TypeActionOwning Principal
GrantExecuteMssExec
SQL Script
create procedure [dbo].[BcImportAccounts]
    @items BcAccountImportItems readonly
as
begin
    set nocount on

        declare @now datetimeoffset = sysdatetimeoffset()

        declare @AccountToBcMap table
        (
            AccountFid int not null,
            BcId uniqueidentifier not null,
            LastUpdatedInBcOn datetimeoffset
        )

        ;with Items as
    (
        select
            BcAccount.AccountingAccountFid,
            ImportItems.CanPost,
            ImportItems.[Description],
            ImportItems.Code,
            ImportItems.BcId,
            ImportItems.LastModified,
            AccountingAccountType.AccountingAccountTypeId
        from @items ImportItems
        inner join AccountingAccountType on ImportItems.AccountTypeName = AccountingAccountType.[Description]
        left outer join BcAccount on ImportItems.BcId = BcAccount.BcId
    )

    merge AccountingAccount with ( tablock ) using Items on Items.AccountingAccountFid = AccountingAccount.AccountingAccountId
    when not matched then
        insert(
            Code,
            [Description],
            CanPost,
            [Hidden],
            AccountingAccountTypeFid )
    values(
        Items.Code,
        Items.[Description],
        Items.CanPost,
        0,
        AccountingAccountTypeId
    )
    when matched then
        update set
            Code = Items.Code,
            [Description] = Items.[Description],
            CanPost = Items.CanPost,
            [Hidden] = 0,
            AccountingAccountTypeFid = AccountingAccountTypeId
    output inserted.AccountingAccountId, Items.BcId, Items.LastModified into @AccountToBcMap
    ;


    ;with Items as
    (
        select
            ImportItems.BcId,
            AccountToBcMap.AccountFid,
            AccountToBcMap.LastUpdatedInBcOn
            from @items ImportItems
            inner join @AccountToBcMap AccountToBcMap on
                ImportItems.BcId = AccountToBcMap.BcId
    )
    merge BcAccount with ( tablock ) using Items on
        Items.AccountFid = BcAccount.AccountingAccountFid
    when not matched then insert( BcId, CreatedOn, LastImportedOn, AccountingAccountFid, LastUpdatedInBcOn ) values( Items.BcId, @now, @now, Items.AccountFid, Items.LastUpdatedInBcOn )
    when matched then update set
        LastImportedOn = @now,
        LastUpdatedInBcOn = Items.LastUpdatedInBcOn
    ;

    --BCTD:  Similar logic to "Hide" things?  Will need to make it so we call this once per import if so.
    --mark anything as [Hidden] that wasn't encountered via this import process ( and un-hide everything else.. )
    --;with HiddenStatus as
    --(
    --    select
    --    BcAccount.AccountingAccountFid,
    --    [Hidden] = convert( bit, case when AccountToBcMap.ExternalDbId is null then 1 else 0 end )
    --    from BcAccount
    --    left outer join @AccountToBcMap AccountToBcMap on BcAccount.ExternalDbId = AccountToBcMap.ExternalDbId
    --)
    --update AccountingAccount set [Hidden] = HiddenStatus.[Hidden]
    --from [Hidden]Status
    --inner join AccountingAccount on HiddenStatus.AccountingAccountFid = AccountingAccount.AccountingAccountID


end
GO
GRANT EXECUTE ON  [dbo].[BcImportAccounts] TO [MssExec]
GO
Uses