Script a SQL Server Database to a .sql File — With the Data, Not Just the Schema (No Wizard Needed)
Here is a restore that "worked". The script ran clean — no errors, nothing red — and rebuilt the database perfectly… except every table came back with zero rows. The shape was copied. The data never was. It is the most common Generate Scripts mistake there is, and it fails silently.
The fix is one setting. But to see it clearly, it helps to know what the Generate Scripts wizard in SSMS actually is: a front-end. Clicking through it does not write your script — it collects your choices, sets them as options on SQL Server's scripting engine (SMO, the Scripter), and lets the engine write the file. In this post we call that engine directly, from the terminal, with Microsoft's own scripter tool — same engine, same output file — and we count the rows before and after, twice. Every output below is from the real run (SQL Server 2025, on Linux).
The map — everything follows it: a database → the Scripter → one .sql file → a new database that proves the copy.
What you need
- A SQL Server you can experiment on (any recent version and edition; the demo ran on SQL Server 2025 Developer on Linux).
- The demo uses the terminal route:
mssql-scripter(Microsoft's scripter CLI —pip install mssql-scripter) andsqlcmd. Every option below is also a checkbox in the SSMS wizard's Advanced page — the wizard equivalents are named at each step, so SSMS users can follow along exactly. - About 15 minutes.
Step 1 — The demo database
BookShelf, a small bookstore database: 6 tables, 34 rows. Authors feed Books; Orders feed OrderItems — a foreign-key chain. It has a view, a stored procedure, a non-clustered index, a trigger, and identity columns numbering every row. Remember 34 — we count again at the end, twice. The seed script is in the companion folder (00-seed-bookshelf.sql).
Source counts, before anything is scripted:
| Table | Rows |
|---|---|
| Authors | 4 |
| Books | 8 |
| Customers | 5 |
| Orders | 6 |
| OrderItems | 10 |
| PriceHistory | 1 |
| Total | 34 |
Step 2 — Run 1: the defaults (the click-through run)
The wizard equivalent: right-click the database → Tasks → Generate Scripts, click through, change nothing, save the file. The engine route — what we run:
mssql-scripter -S localhost,1433 -d BookShelf -U sa -f run1-defaults.sql
Out comes run1-defaults.sql — 12.5 KB. It looks like a finished job. It is not.
Step 3 — Inspect the file: the trap
Open the file: CREATE TABLE, CREATE TABLE — the whole skeleton — and not one single INSERT. Counted, from the real file:
| In run1-defaults.sql | Count |
|---|---|
| INSERT statements | 0 |
| CREATE TABLE | 6 |
| CREATE INDEX | 1 |
| CREATE TRIGGER | 1 |
| FOREIGN KEY | 5 |
The defaults script the schema only. In the wizard, the same choice sits on the Advanced page: "Types of data to script" starts at "Schema only". Your data was never invited.
Step 4 — The honest test: the empty copy
A script like this is self-contained — it creates its own database — so the honest test is brutal and simple: delete the database, and let the file rebuild it. Whatever is not in the file does not come back. The script completes — no errors. Then count:
| Table | Rows after Run 1's rebuild |
|---|---|
| Authors | 0 |
| Books | 0 |
| Customers | 0 |
| Orders | 0 |
| OrderItems | 0 |
| PriceHistory | 0 |
All 6 tables, the view, the procedure, the trigger, the index, all 5 foreign keys — present. 0 rows. A perfect copy of the shape, with nothing inside it.
Step 5 — The six options that decide your file
Every one is a checkbox on the wizard's Advanced page, and a switch on the engine:
| # | Wizard setting (Advanced page) | Engine switch (mssql-scripter) | What it decides |
|---|---|---|---|
| 1 | Types of data to script | (default) vs --schema-and-data | Schema only, or schema + data — the difference between the trap and a real copy |
| 2 | Script indexes | --exclude-indexes turns them off | In the wizard: whole-database scripting includes indexes; pick specific objects and this starts OFF |
| 3 | Script triggers | --exclude-triggers turns them off | In the wizard it starts OFF — and nobody misses a trigger until its work stops happening |
| 4 | Check for object existence | --check-for-existence | Whether each CREATE is wrapped in IF NOT EXISTS — whether the script survives a second run (Step 8) |
| 5 | Script foreign keys | --exclude-foreign-keys turns them off | Whether the chain — OrderItems → Orders → Books — arrives intact |
| 6 | Output: single file / file per object | --file-per-object | One portable file, or a folder of per-object scripts that sits neatly in source control |
Everything else on that page: leave it at the default until you have a reason.
Step 6 — Run 2: the recommended options
mssql-scripter -S localhost,1433 -d BookShelf -U sa -f run2-recommended.sql --schema-and-data --check-for-existence
run2-recommended.sql lands at 20.9 KB — not even twice the size of Run 1. That difference is not formatting. That difference is your data, in the file at last: 34 INSERT statements.
Step 7 — Inside the file: identities and guards
Two details make Run 2's file a faithful copy. First, the INSERTs are wrapped in SET IDENTITY_INSERT [dbo].[Authors] ON … OFF (12 of those lines — one pair per table), so the original identity values travel: Book 7 stays Book 7 on the other side. Second, every CREATE is wrapped in a guard — IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Authors]') …) — the existence checks, standing guard.
Step 8 — The proof, and the second run
Delete the database again. Run Run 2's file. Count:
| Table | Source | Rebuilt from Run 2's file |
|---|---|---|
| Authors | 4 | 4 |
| Books | 8 | 8 |
| Customers | 5 | 5 |
| Orders | 6 | 6 |
| OrderItems | 10 | 10 |
| PriceHistory | 1 | 1 |
| Total | 34 | 34 |
34 of 34 — and the object list matches too: the view, the procedure, the trigger, the index, all five foreign keys. The copy is not claimed. It is counted.
Now run the same file a second time. Run 1's file, re-run, produced 16 "already exists" errors. Run 2's file: not one object error — every CREATE checks first and steps aside. The data half stops at the first primary-key violation, and that is the honest limit of the setting: existence checks guard objects, not rows — a data script is a one-shot restore onto an empty database. And the counts after the re-run? Still exactly 34. Nothing duplicated, nothing lost.
When not to use a script
A scripted copy is for portability, source control, and dev copies. For disaster recovery — point-in-time restore, transaction logs, a production database you cannot afford to lose — use BACKUP / RESTORE. Different tools, different jobs; this post is about the portable file.
The checklist — save this one
- Script the data, not just the schema — "Types of data to script" is the whole game.
- Know your indexes switch — it depends on how you script (whole database vs picked objects).
- Know your triggers switch — in the wizard it starts OFF.
- Keep the foreign keys — the chain is part of the database.
- Check for object existence — if the script may ever run twice.
- Choose the output on purpose — one file, or one per object.
The wizard was never the point; the options were. Next time you script a database — will you check what the defaults left behind, before your restore comes back empty?
Get the code
The companion folder carries the seed script, the counting script, and both generated files — run1-defaults.sql (the trap) and run2-recommended.sql (the copy) — so you can diff them line by line: topic-ssms-generate-scripts







Comments
Post a Comment