0

I use multiple Merge Scripts for CI/CD deployment a database. Dacpac contains Post Deployment Script which is used in Azure Devops Deploy Azure DB Task. This script is a concatenation of multiple merge scripts. I would like to somehow store detailed log of which records was updated/deleted/inserted. In azure release logs I only have an information, that update script was executed (after enabling diagnostics).

Every mergescript is like this:

MERGE INTO [Common].[Address] AS Target
USING (VALUES 
    (N'00000009-0000-0000-0000-000000000001', N'Sample Street', N'Warsaw', N'15-123', N'Mazowieckie', N'Poland')
)
AS Source ([Id], [Street], [City], [PostalCode], [Voivodeship], [Country])
ON Target.[Id] = Source.[Id]

-- update matched rows
WHEN MATCHED AND (
    [Target].[Street] != Source.[Street] OR 
    [Target].[City] != Source.[City] OR
    [Target].[PostalCode] != Source.[PostalCode] OR
    [Target].[Voivodeship] != Source.[Voivodeship] OR 
    [Target].[Country] != Source.[Country]
) THEN
UPDATE SET 
    [Street] = Source.[Street], 
    [City] = Source.[City], 
    [PostalCode] = Source.[PostalCode],
    [Voivodeship] = Source.[Voivodeship], 
    [Country] = Source.[Country]

-- insert new rows
WHEN NOT MATCHED BY TARGET THEN
INSERT ([Id], [Street], [City], [PostalCode], [Voivodeship], [Country])
VALUES (Source.[Id], Source.[Street], Source.[City], Source.[PostalCode], Source.[Voivodeship], Source.[Country])

OUTPUT $action, [deleted].*, [inserted].*;

In the output I have the exact information that I need.

I can create dedicated temporary table, list all needed columns, then insert Output into this table. Then I can use FOR JSON Clause for whole rowset, and save it into another Log table that only have two columns: timestamp and json data, or just print this out. Problem with that soulution is that i have many merge scritps, and I don't want to create dedicated table for every one. Is there a way to initialize temporary table from Output Clouse? Or even serialize output directly into JSON formatted variable?

Chamberlain
  • 881
  • 5
  • 17
  • 1
    If you can not get detailed log of which records was updated/deleted/inserted in your script, then DevOps pipeline would not be able to output the log. It seems your issue is more related to sql script. You need to figure out how to output the information in your script first. So you may add sql-script tag and remove azure-devops tag for better response. – Cece Dong - MSFT Apr 26 '21 at 09:36
  • I never tried it, but if you cannot OUTPUT to JSON directly, maybe you can to XML? Have you tried adding a `FOR XML` clause to the OUTPUT? – FrankPl Apr 26 '21 at 14:53
  • no - XML works the same way as JSON – mateuszwdowiak Apr 26 '21 at 16:15

0 Answers0