Home » Blog » SQL Server » How to Export SQL Server Database to SQL File?

How to Export SQL Server Database to SQL File?

Neeraj Kumar
Published By Neeraj Kumar
Debasish Pramanik
Approved By Debasish Pramanik
Published On
Last Updated

Export SQL Server database to SQL file with the Generate Scripts wizard or the Script as menu. Generate Scripts saves structure by default and needs the data option set to schema and data for rows. Migration software exports a live database to SQL scripts with schema and rows.

You need to export SQL Server database to SQL file for a test server or an archive and SSMS gives you a script without a single row. Two settings in the wizard decide that.

Most guides stop at the wizard pages. Few explain the data setting, the server version and the encoding that decide whether the file works on another server.

This guide explains three ways to save a database as a SQL file and how to use the file on another server.

Microsoft lists every option in its Generate Scripts wizard reference.

Which Method Fits Your Database?

Match your need to a method first:

Your NeedMethod
A full database with rows in one fileMethod #1
One table or a few objects of one typeMethod #2
A live database with a preview and object selectionMethod #3

Types of data to script defaults to Schema only according to Microsoft. Rows appear in the file after you change that setting.

What Should You Prepare First?

Four items prevent a failed file:

  1. Sign in with a login that can access every object you plan to script.
  2. Note the SQL Server version of the destination server because the script must match it.
  3. Create a backup before the move because a script cannot replace one. Explore Different Types of Backups in SQL Server explains the types.
  4. Free disk space for the file because scripts with data grow large.

3 Ways to Export SQL Server Database to SQL File

You get two SSMS options and one software option:

MethodBest ForLimit
Generate Scripts WizardFull databaseSchema only by default
Script as MenuSingle objectsNo table rows
Migration SoftwareLive database with previewTrial exports are capped

Method #1: Export the Database With the Generate Scripts Wizard (Free)

Generate Scripts saves the full database in one file. Eight steps create the file:

  1. Right click your database under Databases in Object Explorer.
  2. Point to Tasks and select Generate Scripts....
  3. Continue past the Introduction page.
  4. Select the option for the entire database on Choose Objects.
  5. Select Save as script file on Set Scripting Options and choose Single script file.
  6. Select Unicode text or ANSI text and type the file path.
  7. Click Advanced and choose schema and data for Types of data to script. Click OK.
  8. Click Next on the Summary page and click Finish on the Save Scripts page.

Note: Set Script for server version to the version of the destination server. Microsoft sets the source version as the default. Check for object existence adds a test before every object so a second use of the file does not fail.

Method #2: Script Single Objects With the Script as Menu (Free)

Script as creates the definition of one object or several objects of one type. Six steps script a table:

  1. Expand the database and the Tables node.
  2. Right click the table and point to Script Table as.
  3. Point to Create to.
  4. Select New Query Editor Window, File or Clipboard as the location.

Several tables need two more steps:

  1. Press F7 to open Object Explorer Details and click the first table. Press Ctrl and click the others.
  2. Right click one selected table and select Script Table as.

Note: Microsoft states that the output uses Unicode format. Script as saves definitions and no rows so choose Method #1 or Method #3 for data.

Method #3: Export a Live Database to SQL Scripts With EmailDoctor (Trial Limit Applies)

EmailDoctor SQL Server Data Migration Tool reaches a live server in Online mode and exports tables, views, stored procedures and triggers to SQL Server compatible scripts. You select the objects and see a preview before the export.

Free mode exports 25 records from every table and 5 other categories according to the vendor.

Connect to the Live Server

Install the software on Windows. Six steps connect and show the objects:

  1. Open the software. Software start screen
  2. Click Open and select Online mode. Online and Offline mode choices
  3. Type the source server and the login in Server Name with Authentication. Source server login window
  4. Open the Drop Down List and select the database. Click OK. Source database list
  5. Check the objects in the preview tree. Database objects preview
  6. Click Export. Export button

Save the SQL Scripts

Three steps create the files:

  1. Select the SQL Server Compatible SQL Scripts option from the list of four. Export options
  2. Tick the objects and choose With Schema & Data for rows or With only Schema for structure.
  3. Click Save and set the destination folder. Close the Export Completed message after the export.

Note: Offline mode scans MDF files that no server has attached. How to Convert MDF File to SQL Script in SQL Server has the steps for it.

How do You Use the SQL File on Another Server?

sqlcmd applies the file in three steps:

  1. Open Command Prompt on a PC that reaches the destination server.
  2. Type the command with your own server and path:
sqlcmd -S SQLHOST02 -E -i "C:\scripts\SalesDB.sql" -b
  1. Check the messages for errors.

-i names the file and -b stops with an error level when an error occurs. Add -x when the data contains text in the $(name) format because sqlcmd treats it as a scripting variable.

Note: Microsoft states that quotation marks must surround paths with spaces. -i and -Q cannot work together.

Which Problems Break the Exported File?

Check these five failures before you blame the wizard:

ProblemReasonFix
The file has no rowsTypes of data to script defaults to Schema onlySelect schema and data
The script fails on an older serverThe script was made for a newer server versionSet Script for server version to the destination version
Dependent objects are missingDependent objects are off for selected objectsScript the entire database
The wizard stops at an errorContinue scripting on error is offSet it to True
Text with $(name) fails in sqlcmdsqlcmd treats it as a variableAdd -x

Frequently Asked Questions

Does Generate Scripts Include the Data?

No. Types of data to script defaults to Schema only according to Microsoft. Change it to include rows.

Can I Save the Full Database in One File?

Yes. Select Single script file on Set Scripting Options. One script file per object creates many files.

Is mssql-scripter Still Available?

Microsoft archived the repository in May 2024 and calls the tool no longer actively maintained. Use the Generate Scripts wizard instead.

Why Does the SQL File Show Strange Characters?

Your editor opens the file with another encoding. Select Unicode text for non English data and open the file in an editor that handles it.

How do I Keep the Old File When I Export Again?

Use a new file name. Overwrite existing file replaces the old file and Append to file under Advanced is off by default.

Can I Export Logins With the Database?

Yes. Script logins and Script object-Level permissions are off by default. Set both to True under Advanced.

Does the Script as Menu Include Table Rows?

No. Script as saves only the definition of the object. Use the wizard with Types of data to script for rows.

Can I Try the Migration Software Before I Buy?

Yes. Free mode stops at 25 records from every table and 5 other categories.

Author’s Verdict

Use the wizard for a full database and check Types of data to script before you click Finish. Match the server version to the destination.

Use the Script as menu for one table and migration software when you want a preview and object selection.

Test the file on a scratch server with sqlcmd before any production use.

  • Generate Scripts exports a full database to one SQL file and needs the data setting for rows.
  • Script as exports definitions only.
  • Migration software exports a live database to SQL scripts with a preview.
  Neeraj Kumar

By Neeraj Kumar

Neeraj Kumar is a Principal Technical Architect at EmailDoctor with 19+ years of experience in email migration, cloud migration, data recovery and digital forensics. He guides enterprise teams through Exchange, Office 365 and Google Workspace projects and shares what works as an EmailDoctor writer. Every method in his posts is tested on Windows and Mac before it goes live.