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
;
end
GO
GRANT EXECUTE ON [dbo].[BcImportAccounts] TO [MssExec]
GO