Files
Fuchs_Intranet/Fuchs_Database/dbo/Stored Procedures/mfr__updt__invoices.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

83 lines
2.4 KiB
Transact-SQL

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[mfr__updt__invoices]
@tblname as nvarchar(50)
, @referencetable nvarchar(50)
, @tgtid bigint
AS
BEGIN
SET NOCOUNT ON;
DECLARE @tmp [dbo].[mfr__tt__invoices];
DECLARE @tmp_cmd nvarchar(1000) = N'SELECT DISTINCT * FROM ' + @tblname
INSERT INTO @tmp EXECUTE [sp_executesql] @tmp_cmd;
MERGE [dbo].[mfr__invoices] as TARGET
USING @tmp as SOURCE ON TARGET.[Id] = SOURCE.[Id]
WHEN MATCHED AND (TARGET.[Version] <= SOURCE.[Version] OR TARGET.[DateOfCreation] <= SOURCE.[DateOfCreation] ) THEN
UPDATE
SET [Id] = SOURCE.[Id]
,[Version] = SOURCE.[Version]
,[DateOfCreation] = SOURCE.[DateOfCreation]
,[URI] = SOURCE.[URI]
,[DocumentName] = SOURCE.[DocumentName]
,[ReportDefinitionCode] = SOURCE.[ReportDefinitionCode]
,[FileType] = SOURCE.[FileType]
,[InvoiceBalance] = SOURCE.[InvoiceBalance]
,[InvoiceBalanceNetto] = SOURCE.[InvoiceBalanceNetto]
,[WageBalanceNet] = SOURCE.[WageBalanceNet]
,[DatePayed] = SOURCE.[DatePayed]
,[InvoiceId] = SOURCE.[InvoiceId]
,[DueDate] = SOURCE.[DueDate]
,[WithoutVAT] = SOURCE.[WithoutVAT]
,[Skonto] = SOURCE.[Skonto]
,[PartialPayment] = SOURCE.[PartialPayment]
,[InvoiceState] = SOURCE.[InvoiceState]
,[Note] = SOURCE.[Note]
WHEN NOT MATCHED BY TARGET THEN
INSERT
([Id]
,[Version]
,[DateOfCreation]
,[URI]
,[DocumentName]
,[ReportDefinitionCode]
,[FileType]
,[InvoiceBalance]
,[InvoiceBalanceNetto]
,[WageBalanceNet]
,[DatePayed]
,[InvoiceId]
,[DueDate]
,[WithoutVAT]
,[Skonto]
,[PartialPayment]
,[InvoiceState]
,[Note]
,[file_guid])
VALUES
( SOURCE.[Id]
,SOURCE.[Version]
,SOURCE.[DateOfCreation]
,SOURCE.[URI]
,SOURCE.[DocumentName]
,SOURCE.[ReportDefinitionCode]
,SOURCE.[FileType]
,SOURCE.[InvoiceBalance]
,SOURCE.[InvoiceBalanceNetto]
,SOURCE.[WageBalanceNet]
,SOURCE.[DatePayed]
,SOURCE.[InvoiceId]
,SOURCE.[DueDate]
,SOURCE.[WithoutVAT]
,SOURCE.[Skonto]
,SOURCE.[PartialPayment]
,SOURCE.[InvoiceState]
,SOURCE.[Note]
,newid());
END