Files
Fuchs_Intranet/Fuchs_Database/dbo/Stored Procedures/mfr__updt__companies.sql
T
Stefan 10ecdfa2e4 Add Fuchs_Database SSDT project (schema source of truth)
Adds the SQL Server Data Tools project for the fuchs_fds database — tables,
table types, functions and stored procedures that the backend calls (e.g.
fds__getInvoice, fds__merge_bankingtransactions, fds__tt__bankingtransactions,
fds__admin_getReportCatalog, fis_* auth). Build/model caches (bin, obj,
*.dbmdl, *.jfm, *.user) are git-ignored.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-05 14:50:54 +02:00

102 lines
3.2 KiB
Transact-SQL

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[mfr__updt__companies]
@tblname as nvarchar(50)
, @referencetable nvarchar(50)
, @tgtid bigint
AS
BEGIN
SET NOCOUNT ON;
DECLARE @now datetime = GETUTCDATE();
DECLARE @tmp [dbo].[mfr__tt__companies];
DECLARE @tmp_cmd nvarchar(1000) = N'SELECT DISTINCT * FROM ' + @tblname
INSERT INTO @tmp EXECUTE [sp_executesql] @tmp_cmd;
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
MERGE [dbo].[mfr__companies] as TARGET
USING @tmp as 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]);
END