130 lines
4.8 KiB
Transact-SQL
130 lines
4.8 KiB
Transact-SQL
-- =============================================
|
|
-- Description: mfr sync — merges one staged batch of Company entities into [mfr__companies].
|
|
-- @tblname is the bulk-copied staging table (same layout as [mfr__tt__companies]);
|
|
-- [setid] is the batch marker. A full entity reset (@referencetable = 'mfr__companies')
|
|
-- first drops the company's dependent partner/custom-value/location/comment rows so
|
|
-- they are rebuilt from the same batch. Duplicate Ids within one batch (paged reads
|
|
-- overlapping a concurrent mfr edit) are collapsed to the highest Version before the
|
|
-- MERGE, otherwise MERGE raises 8672.
|
|
-- =============================================
|
|
CREATE PROCEDURE [dbo].[mfr__updt__companies]
|
|
@tblname as nvarchar(50)
|
|
, @referencetable nvarchar(50)
|
|
, @tgtid bigint
|
|
AS
|
|
BEGIN
|
|
SET NOCOUNT ON;
|
|
SET XACT_ABORT ON;
|
|
|
|
BEGIN TRY
|
|
|
|
DECLARE @tmp [dbo].[mfr__tt__companies];
|
|
|
|
DECLARE @tmp_cmd nvarchar(1000) = N'SELECT DISTINCT * FROM ' + @tblname
|
|
INSERT INTO @tmp EXECUTE [sp_executesql] @tmp_cmd;
|
|
|
|
-- Wrapping the DELETEs + MERGE below in one transaction gives all-or-nothing atomicity for
|
|
-- this entity's sync batch (same pattern in every mfr__updt__* proc). During a large full
|
|
-- reset this can hold locks on the affected tables for as long as the batch takes to run —
|
|
-- an accepted, documented risk, not an oversight. See
|
|
-- Docs/Decisions/0022-mfr-sync-transaction-blocking-accepted-risk.md before changing this.
|
|
BEGIN TRANSACTION;
|
|
|
|
IF @referencetable = 'mfr__companies'
|
|
BEGIN
|
|
DELETE p
|
|
--OUTput deleted.*, @now INTO [dbo].[mfr__d_*PartnerSet]
|
|
FROM [dbo].[mfr__*PartnerSet] as p
|
|
WHERE p.Property like 'Company[:]%' AND EXISTS (SELECT * FROM @tmp as tmp where tmp.[id] = p.[EntityId]);
|
|
DELETE cv
|
|
--OUTput deleted.*, @now INTO [dbo].[mfr__d_#customvalues]
|
|
FROM [dbo].[mfr__#customvalues] as cv
|
|
WHERE cv.Property like 'Company[:]%' AND EXISTS (SELECT * FROM @tmp as tmp where tmp.[id] = cv.[EntityId]);
|
|
DELETE lc
|
|
--OUTPUT deleted.*, @now INTO [dbo].[mfr__d_#locations]
|
|
FROM [dbo].[mfr__#locations] as lc
|
|
WHERE lc.Property like 'Company[:]%' AND EXISTS (SELECT * FROM @tmp as tmp where tmp.[id] = lc.[EntityId]);
|
|
DELETE cm
|
|
--OUTPUT deleted.*, @now INTO [dbo].[mfr__d_comments]
|
|
FROM [dbo].[mfr__comments] as cm
|
|
WHERE EXISTS (SELECT * FROM @tmp as tmp where tmp.[id] = cm.[CompanyId] AND ISNULL(cm.[ServiceRequestId],0) = 0);
|
|
END;
|
|
|
|
WITH tmp as (
|
|
SELECT *, ROW_NUMBER() OVER (PARTITION BY [Id] ORDER BY [Version] DESC, [DateModified] DESC) as '#' FROM @tmp
|
|
), SOURCE as (
|
|
SELECT * FROM tmp WHERE [#] = 1
|
|
)
|
|
MERGE [dbo].[mfr__companies] as TARGET
|
|
USING SOURCE ON TARGET.[Id] = SOURCE.[Id]
|
|
WHEN MATCHED AND (TARGET.[Version] <= SOURCE.[Version] OR TARGET.[DateModified] <= SOURCE.[DateModified]) THEN
|
|
UPDATE
|
|
SET [Id] = SOURCE.[Id]
|
|
,[Location#ID] = SOURCE.[Location#ID]
|
|
,[Version] = SOURCE.[Version]
|
|
,[IsPhysicalPerson] = SOURCE.[IsPhysicalPerson]
|
|
,[IsOwner] = SOURCE.[IsOwner]
|
|
,[IsEmailInvoicingActive] = SOURCE.[IsEmailInvoicingActive]
|
|
,[IsSupplier] = SOURCE.[IsSupplier]
|
|
,[MappingId] = SOURCE.[MappingId]
|
|
,[ExternalId] = SOURCE.[ExternalId]
|
|
,[Name] = SOURCE.[Name]
|
|
,[Note] = SOURCE.[Note]
|
|
,[SupportTelephone] = SOURCE.[SupportTelephone]
|
|
,[SupportFax] = SOURCE.[SupportFax]
|
|
,[SupportMail] = SOURCE.[SupportMail]
|
|
,[QuickSearch] = SOURCE.[QuickSearch]
|
|
,[DateModified] = SOURCE.[DateModified]
|
|
,[CustomValues] = SOURCE.[CustomValues]
|
|
,[MainContactId] = SOURCE.[MainContactId]
|
|
WHEN NOT MATCHED BY TARGET THEN
|
|
INSERT
|
|
( [Id]
|
|
,[Location#ID]
|
|
,[Version]
|
|
,[IsPhysicalPerson]
|
|
,[IsOwner]
|
|
,[IsEmailInvoicingActive]
|
|
,[IsSupplier]
|
|
,[MappingId]
|
|
,[ExternalId]
|
|
,[Name]
|
|
,[Note]
|
|
,[SupportTelephone]
|
|
,[SupportFax]
|
|
,[SupportMail]
|
|
,[QuickSearch]
|
|
,[DateModified]
|
|
,[CustomValues]
|
|
,[MainContactId])
|
|
VALUES
|
|
(SOURCE.[Id]
|
|
,SOURCE.[Location#ID]
|
|
,SOURCE.[Version]
|
|
,SOURCE.[IsPhysicalPerson]
|
|
,SOURCE.[IsOwner]
|
|
,SOURCE.[IsEmailInvoicingActive]
|
|
,SOURCE.[IsSupplier]
|
|
,SOURCE.[MappingId]
|
|
,SOURCE.[ExternalId]
|
|
,SOURCE.[Name]
|
|
,SOURCE.[Note]
|
|
,SOURCE.[SupportTelephone]
|
|
,SOURCE.[SupportFax]
|
|
,SOURCE.[SupportMail]
|
|
,SOURCE.[QuickSearch]
|
|
,SOURCE.[DateModified]
|
|
,SOURCE.[CustomValues]
|
|
,SOURCE.[MainContactId]);
|
|
|
|
COMMIT TRANSACTION;
|
|
END TRY
|
|
BEGIN CATCH
|
|
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
|
|
|
|
INSERT INTO dbo.[fds__admin_dberrors] ([UserName], [ErrorNumber], [ErrorState], [ErrorSeverity], [ErrorLine], [ErrorProcedure], [ErrorMessage], [ErrorDateTime])
|
|
VALUES (SUSER_SNAME(), ERROR_NUMBER(), ERROR_STATE(), ERROR_SEVERITY(), ERROR_LINE(), ERROR_PROCEDURE(), ERROR_MESSAGE(), GETDATE());
|
|
|
|
THROW;
|
|
END CATCH;
|
|
END |