Quick Tips to Convert & Export MDF to SQL Script

MDF Files

SQL Server uses primary database files, also called as Master Database Files(MDF) to store data and schema. MDF files stores all the database information including the rows, columns, fields, and data created by the user. All the information related to creation, modification, updations of databases are stored here. The data is stored in MDF files with the .mdf file extension. But the problem with the MDF files is that it cannot be opened as such by the users so as to view the queries or commands. In order to read the data, users can convert MDF to SQL Script.

SQL Script

SQL script is a set of commands and statements that are saved as a file in SQL scripts. The scripts are stored in .sql files. These can be used to create, view, edit and even delete the script files and contain one or more commands and statements. For example, SQL script may contain a statement to CREATE a table, INSERT statement to insert data into the tables, another statement to SELECT all the data that was inserted into the table and more. It can also create the whole database itself with the entire tables, data etc.

Need to Convert MDF file to SQL Script

The MDF files are the primary database files in SQL server, but you cannot really open these files, instead, you have to attach them in order to view it. But if you attach using unknown or untrusted sources, then it could contain malicious codes and may cause errors. So a better option is to create SQL script from MDF file which provides the documentation of the data can also be used to create, view, add the script files.

Since the SQL scripts can be used to update and run queries in our database, the MDF files can be converted to .sql files. The most commonly asked query among the users is "How to script out .mdf file to .sql file?". Here, we would discuss the steps to export MDF file to .sql using an MDF to SQL script converter.

Steps to Create SQL script from MDF file

Step 1

To convert MDF file to SQL, go to the SQL script software menu

  • Click on Open
  • Select and open the required MDF files from its location
Open

Step 2

Perform Quick scan

Quick Scan

Step 3

Preview Database items: The SQL Script Software will show the entire MDF files including tables, triggers, stored procedures, views etc.

Preview

Follow the steps below to export MDF to SQL script file

Step 4

SQL Script files are saved as .sql files. So, to create SQL script from MDF file,

  • Go to Export to begin the export process
  • Choose SQL Server Compatible SQL Script
Create SQL Script

MDF to SQL Converter will efficiently generate the script of the selected database and now this format is ready to export.

Step 5

Now, the user has various options to select and export only the required components like tables, views, stored procedures etc.

Components

Step 6

Choose according to need, the MDF to SQL converter gives the option to create SQL Script from MDF file With only Schema or With Schema and Data

Schema Option

Step 7

With only schema will generate sql script from the .mdf file of the selected table, views of the schema. And the data within tables will be removed.

Only Schema

  • After this, a confirmation window will pop up to confirm whether to export SQL Server compatible script With only schema.

  • with Schema Confirm

  • Then, Choose the location to save the .sql file

  • Save

  • Now, a window will pop up displaying Export Completed confirming the successful export of the database
  • Click OK

  • Export Completed

  • The software will show the status of the completion process
  • Click Close

  • Close

Now you have exported MDF to SQL script with the only schema. To get both schema and data have a look on next step.

Step 8

To create SQL script from MDF file with schema and the table data , checked the option With Schema and Data, then click on Save

Schema With Data

  • Now a window will pop up to enquire whether to export records as well?
  • Click on Yes

  • Export Records

  • Now choose the location to save .sql file

  • Save SQL File

  • Now a window will pop up displaying Export Completed
  • Click OK

  • Saved

  • Status of the completion process will be shown
  • Click onClose

  • Close Script

Now the process to convert MDF file to .sql files is completed with schema and data.

Conclusion

Many of the database administration tasks can be performed via SQL scripts. Throughout this article we have gone through super easy tips to convert MDF file to SQL Script. And discussed basic steps to export MDF to SQL Script after conversion effectively.