A lot of business logic still lives in stored procedures. Pricing, discounts, stock, invoicing. In most of the codebases I've seen, those procedures have no tests, and everyone is a little afraid to touch them.
Now we also ask AI agents to "clean up" or "optimize" them. The result usually looks better. Sometimes it also behaves differently, and nobody notices until a customer does.
This post walks through one small example of that, and the open source tool I built to catch it: sprocguard.
The procedure
Here is a simplified order procedure. VIP customers get 10% off, rounded to two decimals:
SET @Total = @Price * @Quantity; IF EXISTS (SELECT 1 FROM dbo.Customers WHERE CustomerId = @CustomerId AND Segment = 'VIP') SET @Total = ROUND(@Total * 0.90, 2); -- VIP customers get 10% off INSERT INTO dbo.Orders (CustomerId, OrderDate, Total) VALUES (@CustomerId, SYSDATETIME(), @Total); A coffee costs 12.50. A VIP buys 3: 37.50 minus 10% is 33.75.
The "harmless" refactor
Someone, human or agent, decides two statements are one too many:
-- "Simplified": one expression instead of two steps. SET @Total = CAST(@Price * @Quantity * CASE WHEN EXISTS (SELECT 1 FROM dbo.Customers WHERE CustomerId = @CustomerId AND Segment = 'VIP') THEN 0.9 ELSE 1 END AS decimal(10,1)); It reads fine. It compiles. Regular customers get the same totals. But decimal(10,1) keeps one decimal, so 33.75 becomes 33.80. The VIP now pays five cents more, and the wrong total is saved in dbo.Orders.
Code review probably won't catch this. The diff looks like a cleanup, and the bug only shows up for one customer segment with a specific quantity.
Characterization tests in a few minutes
The idea behind sprocguard is old: characterization tests, also called golden master tests. You don't write assertions by hand. You record what the code does today and compare every new version against that record.
For each case (a set of parameter values), sprocguard records:
- every result set;
- the return value and OUTPUT parameters;
- the error raised, if any, because errors are behavior too;
- the rows inserted, updated and deleted in each table the procedure writes.
Cases are a small JSON file:
[ { "name": "regular customer buys 2 coffees", "params": { "@CustomerId": 2, "@ProductId": 10, "@Quantity": 2 } }, { "name": "VIP customer gets 10% off", "params": { "@CustomerId": 1, "@ProductId": 10, "@Quantity": 3 } }, { "name": "VIP discount with odd cents", "params": { "@CustomerId": 1, "@ProductId": 10, "@Quantity": 7 } }, { "name": "buys exactly the last units", "params": { "@CustomerId": 2, "@ProductId": 11, "@Quantity": 2 } }, { "name": "insufficient stock raises error", "params": { "@CustomerId": 2, "@ProductId": 11, "@Quantity": 5 } } ] Then:
pip install sprocguard sprocguard init # writes sprocguard.toml; set your connection sprocguard capture dbo.usp_PlaceOrder --cases cases/usp_PlaceOrder.json # ...deploy the refactor... sprocguard verify dbo.usp_PlaceOrder And the verify output:
FAIL dbo.usp_PlaceOrder: 3/5 case(s) behave exactly as in the baseline. (the procedure definition changed since the baseline was captured) ok regular customer buys 2 coffees XX VIP customer gets 10% off - Result set #1, row 1: Total "33.75" -> "33.80" - dbo.Orders: no longer inserts {"CustomerId": 1, "Total": "33.75"} - dbo.Orders: now inserts {"CustomerId": 1, "Total": "33.80"} XX VIP discount with odd cents - Result set #1, row 1: Total "78.75" -> "78.80" - dbo.Orders: no longer inserts {"CustomerId": 1, "Total": "78.75"} - dbo.Orders: now inserts {"CustomerId": 1, "Total": "78.80"} ok buys exactly the last units ok insufficient stock raises error It doesn't just say "something changed". It shows which case changed, the old and new values, and the row that would now be written differently.
The hard parts
A few things took more work than I expected.
Not touching your data. Every case runs inside a transaction that is always rolled back. sprocguard only connects to databases listed in allowed_databases, and it refuses to run procedures with side effects a rollback can't undo, such as xp_cmdshell, sp_send_dbmail or linked servers.
Values that change on their own. SYSDATETIME() and IDENTITY values are different on every run, so a naive comparison would always fail. sprocguard runs each case twice, learns which columns change by themselves and ignores them. IDENTITY values are not reused after a rollback in SQL Server, so they count as volatile too.
Knowing which tables to compare. It reads the procedure's dependencies and its INSERT/UPDATE/DELETE/MERGE targets, including tables written by triggers. For dynamic SQL, which it can't see into, you can list extra tables with watch_tables.
Three ways to use it
- Terminal: the commands above, while you work on a change.
- AI agents: it is also an MCP server, so an agent like Claude Code can capture a baseline before it edits SQL and verify afterwards. The agent gets the same diff you saw above, and can fix its own mistake.
- Pull requests: a GitHub Action starts a throwaway SQL Server in Docker, builds your database from the scripts in the repo and runs
verify. Withmode: base-branch, it captures the baselines from the base branch in the same run, so you don't have to commit anything:
- uses: actions/checkout@v7 - uses: JheysonJhordyHuarauyaRiquelme/sprocguard@v0.3.1 with: mode: base-branch cases: db/cases setup-scripts: | db/schema.sql db/seed.sql db/procedures/*.sql If a procedure changed behavior, the pull request fails and the job summary lists every difference.
What's next
It's MIT licensed and on PyPI, the MCP Registry and the GitHub Marketplace. On the roadmap: table-valued parameters, sprocguard impact <table> to verify every procedure that uses a table you're changing, and PostgreSQL support.
Five people I'd never met have already contributed, and there are open issues labeled good first issue if you'd like to be next. If you work with SQL Server, I'd love to hear which procedure you're most afraid to touch, and what sprocguard would need to handle it.