Version-controlling SQL Server queries means storing every change to your scripts in a VCS (Git) and deploying them with repeatable migration files.
Storing each SQL script in Git lets teams track history, review changes, and roll back safely. Pair scripts with numbered migration files so every environment stays in sync.
Create one file per change using the pattern YYYYMMDDHHMM_description.sql
. Prefix with an incremental timestamp; add a brief, kebab-cased description.Commit each file immediately after it is tested.
Popular options include Flyway, Liquibase, dbt, and simple in-house runners that execute files in timestamp order. All tools expect an internal table (e.g., __schema_migrations
) to record applied versions.
Guard every DDL change with IF NOT EXISTS
/IF EXISTS
checks. This prevents errors if the script accidentally runs twice and keeps CI pipelines green.
Yes.Store SELECT statements in the same repo—inside a /queries
folder. Galaxy Collections make sharing endorsed, read-only analytics queries trivial.
Combine only tightly-related DDL in a single migration. Small, atomic files simplify code review and allow partial rollbacks.
Use BEGIN TRY / CATCH
blocks so failures automatically trigger ROLLBACK
, leaving the database consistent.
.
No. Keep SQL next to application code if deploy cycles align; otherwise create a dedicated database
repo.
Create a new migration on a hotfix branch, deploy to prod, then merge back to main so lower environments catch up.