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.

The map — 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) and sqlcmd. 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).

Terminal — seeding the BookShelf demo database: 6 tables, 34 rows

Source counts, before anything is scripted:

TableRows
Authors4
Books8
Customers5
Orders6
OrderItems10
PriceHistory1
Total34

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.

Terminal — Run 1 with the default options, and the 12.5 KB file it produced

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.sqlCount
INSERT statements0
CREATE TABLE6
CREATE INDEX1
CREATE TRIGGER1
FOREIGN KEY5

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.

Code — run1-defaults.sql: CREATE TABLE statements and not one INSERT

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:

TableRows after Run 1's rebuild
Authors0
Books0
Customers0
Orders0
OrderItems0
PriceHistory0

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.

Counts after rebuilding from Run 1's file — all 6 tables present, 0 rows

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
1Types of data to script(default) vs --schema-and-dataSchema only, or schema + data — the difference between the trap and a real copy
2Script indexes--exclude-indexes turns them offIn the wizard: whole-database scripting includes indexes; pick specific objects and this starts OFF
3Script triggers--exclude-triggers turns them offIn the wizard it starts OFF — and nobody misses a trigger until its work stops happening
4Check for object existence--check-for-existenceWhether each CREATE is wrapped in IF NOT EXISTS — whether the script survives a second run (Step 8)
5Script foreign keys--exclude-foreign-keys turns them offWhether the chain — OrderItems → Orders → Books — arrives intact
6Output: single file / file per object--file-per-objectOne 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.

Code — run2-recommended.sql: the INSERTs wrapped in SET IDENTITY_INSERT, every CREATE guarded

Step 8 — The proof, and the second run

Delete the database again. Run Run 2's file. Count:

TableSourceRebuilt from Run 2's file
Authors44
Books88
Customers55
Orders66
OrderItems1010
PriceHistory11
Total3434

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.

Counts after rebuilding from Run 2's file — 34 of 34 rows, matching the source

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

  1. Script the data, not just the schema — "Types of data to script" is the whole game.
  2. Know your indexes switch — it depends on how you script (whole database vs picked objects).
  3. Know your triggers switch — in the wizard it starts OFF.
  4. Keep the foreign keys — the chain is part of the database.
  5. Check for object existence — if the script may ever run twice.
  6. 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

Enjoying these tutorials?

Get new Visual Studio Learn posts by email — free, no spam:

Follow along: YouTube YouTube · Facebook Facebook · Instagram Instagram · TikTok TikTok · RSS RSS

Comments