fix: update mfr__updt__companies procedure and mfr__tt__companies type for improved data handling
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
-- =============================================
|
||||
-- Author: <Author,,Name>
|
||||
-- Create date: <Create Date,,>
|
||||
-- Description: <Description,,>
|
||||
-- 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)
|
||||
@@ -14,7 +18,6 @@ BEGIN
|
||||
|
||||
BEGIN TRY
|
||||
|
||||
DECLARE @now datetime = GETUTCDATE();
|
||||
DECLARE @tmp [dbo].[mfr__tt__companies];
|
||||
|
||||
DECLARE @tmp_cmd nvarchar(1000) = N'SELECT DISTINCT * FROM ' + @tblname
|
||||
@@ -45,11 +48,15 @@ BEGIN
|
||||
--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
|
||||
|
||||
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 @tmp as SOURCE ON TARGET.[Id] = SOURCE.[Id]
|
||||
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]
|
||||
|
||||
@@ -16,6 +16,6 @@
|
||||
[SupportMail] NVARCHAR (255) NULL,
|
||||
[QuickSearch] NVARCHAR (255) NULL,
|
||||
[DateModified] DATETIME NOT NULL,
|
||||
[CustomValues] NVARCHAR (MAX) NOT NULL,
|
||||
[MainContactId] BIGINT NOT NULL);
|
||||
[CustomValues] NVARCHAR (MAX) NULL,
|
||||
[MainContactId] BIGINT NULL);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user