Overview
In SQL Server, every database contains .mdf files which is a primary database file also known as Master Database File. MDF Files contains all the information of SQL Server Database objects like tables,views,triggers etc. Whereas, the .sql Script is a set of command which contains one or more SQL statements. These Scripts can be used for further modification or regeneration of database objects.
Why to Convert MDF to SQL Script
There are various reasons why you need to convert mdf file to .sql script which are as follows:
- By converting .mdf file to .sql script, one can use these files to different servers.
- Sometimes users attach MDF files to the database by some untrusted sources, which may result in error as it could contain malicious information. So at this time we can convert mdf to sql script and execute these scripts on SQL Server for creating that database .
How to Convert .mdf File to .sql Script
You can try different methods for converting MDF Files into .sql as per your requirement. They are as follows:
- Manual method
- Alternate method
Manual Method to Convert MDF into SQL Script
- Launch Microsoft SQL Server Management Studio and connect to the SQL Server.
- Click on the Database option and select the database for which you want to generate .sql script.
- Now right click to your Database, you will get the option ‘Tasks’ followed by ‘Generate Scripts’.Click on that.
- After clicking on ‘Generate Scripts’, you will get the ‘Introduction’ screen in which you have given steps to generate the script. Click on ‘Next’ to proceed.
- After that you will get the ‘Choose Objects’ screen in which two options have been mentioned. Choose ‘Script entire database and all database objects’ option to select the whole database objects, and then click Next.
- SQL Server provides you three different options through which data can be exported i.e. ‘Data only’, ‘Schema only’ and ‘Data and Schema’. Choose the type of data according to your need. For this you have to click on 'Advanced' button.
- After clicking on it, you will get the ‘Advance scripting options’ screen, in which you can select the type of data. Click ‘Type of data in script’ option and choose its type.
- After choosing the type, you will get the screen of ‘Review your selection’, in which you will get to know about your server name, database, its selected objects and the target location of your database.
- After that you will get the report of your database objects which you have generated using ‘Generate Script’ option.
Alternate Method to Convert MDF to SQL Script
You can use alternate option for converting MDF Files to .sql script by using professional tool name SQL Recovery Software.
For this, here is the easy steps to convert mdf file to sql :
- Launch SQL Recovery and Click on ‘Open’ button to upload mdf files.
- After uploading mdf files, Go to 'Export' button to save the files.
- You will get Export options after clicking on ‘Export’ button, Click on ‘SQL Server Compatible SQL Scripts’ to export mdf to sql script. It saves your script to the local system.
- Choose the type of database object accordingly.
- Click ‘With Schema’ to create only schema for tables,views,triggers etc Or ‘With Schema and Data’ to export schema and data for table. Choose 'With Schema and Data’
- Choose the location to save the .sql file.
- A pop up with ‘Export Completed’ confirming the successful export of the database.
Conclusion
In this blog, I have discussed why there is need to convert mdf file to .sql script. The advantage of converting to sql script is that we can use mdf files to different servers. The blog also discusses the possible solutions on how to convert MDF to SQL script manually or by using third party tool in pictorial representation.
EmoticonEmoticon