Lesson 2 of 4 SQL Database Projects

Managing Reference Data in Source Control

This lesson explains how SQL projects deploy reference data with idempotent post-deployment MERGE scripts.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify which data belongs in source control.
  • Explain why post-deployment data scripts must be idempotent.
  • Describe how to organize and parameterize data scripts.

What counts as reference data

Reference data is the small, slowly changing data that code depends on: order statuses, country codes, product categories, configuration rows and permission mappings. If code checks Status = 'Shipped', the Shipped row must exist in every environment with the same key. Business data, such as customers and orders, does not belong in source control, and neither does large or frequently changing data.

Sort into groups

Reference data or not?

Exam skills 2.3.2

Decide whether each data set belongs in source control.

Log in to try this activity Interactive activities are free with an account.

Post-deployment MERGE scripts

The schema model does not contain data, so projects use a post-deployment script, which SqlPackage runs after every schema deployment. Because it runs every time, it must be idempotent: running it repeatedly produces the same result. A MERGE from a VALUES list achieves this by inserting missing rows, updating changed rows and, optionally, deleting rows no longer listed.

-- Scripts/Data/OrderStatus.sql, included from Script.PostDeployment.sql with  :r .\Data\OrderStatus.sql
MERGE Sales.OrderStatus AS target
USING (VALUES
    (1, N'Open'),
    (2, N'Shipped'),
    (3, N'Delivered'),
    (4, N'Cancelled')
) AS source (StatusID, Name)
ON target.StatusID = source.StatusID
WHEN MATCHED AND target.Name <> source.Name THEN
    UPDATE SET Name = source.Name
WHEN NOT MATCHED BY TARGET THEN
    INSERT (StatusID, Name) VALUES (source.StatusID, source.Name)
WHEN NOT MATCHED BY SOURCE THEN
    DELETE;
Match the pairs

Inside the MERGE

Exam skills 2.3.2

Match each MERGE clause with what it does to the reference table.

Log in to try this activity Interactive activities are free with an account.

Organizing and parameterizing data scripts

A project has one post-deployment script, which includes per-table files with the SQLCMD :r directive so each reference table has its own reviewed file. Order matters when tables reference each other: parents are merged before children. Values that differ by environment, such as a feature flag, use SQLCMD variables ($(EnableBeta)) declared in the project and supplied by the publish profile or pipeline. Deleting rows with WHEN NOT MATCHED BY SOURCE should be used only when the table is fully owned by the script, because rows added by users would be removed.

Scenario questions

Organizing data scripts

Exam skills 2.3.2

Choose how to organize reference data scripts.

Log in to try this activity Interactive activities are free with an account.
Worked example

Statuses missing in production

A release adds a 'Returned' order status, inserted manually in development. In production the application fails because the row is missing. The team wants status rows deployed automatically with every release and never duplicated. What should the developer do?

Add a MERGE for the OrderStatus table, listing every status including Returned, to the project's post-deployment script (through an :r include). Each deployment then brings every environment to the same set of rows, and repeated runs do not duplicate them. A plain INSERT script would fail or duplicate rows on the second deployment, and a pre-deployment script runs before the table changes exist.

Check yourself

Why must a post-deployment data script be idempotent?

Show answer

It runs on every deployment, so it must produce the same result each time without duplicates or errors.

Exam Tips

Remember for the exam

  • Reference data: idempotent MERGE in the post-deployment script.
  • One post-deployment script per project; split with :r includes.
  • Environment-specific values: SQLCMD variables.
  • Only use NOT MATCHED BY SOURCE DELETE when the script owns the whole table.