How to: Import Database Objects from a Script
In addition to being able to import your database schema from an existing database, you can also import database objects from one or more existing scripts. You could use this approach, for example, to take an existing database definition that was created by using other third-party tools and import it into your database project. The script that you specified is parsed, and any statements that create database objects are identified and imported into the database project. You can import scripts by using the Import SQL Script File wizard or by using an automation command from the Command Window.
If any object definition contains an error, the object is skipped. If the error is related to the type definition, the statement is put into the ScriptsIgnoredOnImport.sql file in the database project. If the object type is valid but the definition of the object contains an error, such as a view that references a non-existent table, an error message appears in the Error List window.
Statements that are not imported are placed into the ScriptsIgnoredOnImport.sql file in your solution.
Importing Database Object Definitions
To use the Import SQL Script File wizard
On the Project menu, click Import Script.
The Import SQL Script File wizard appears.
Note
You can also right-click your database project in Solution Explorer or Schema View, and click Import Script.
Click Next to advance to the Select File page after you read the introductory page of the wizard.
If you want to import multiple scripts, go to step 7.
Click Single file.
In File name, type the file name, including the path, of the script that you want to import. You can also click Browse to find the file.
Go to step 11.
Click Multiple files.
Click Browse and navigate to the folder that contains the folder that contains the scripts that you want to import.
Important
You are selecting the folder that contains the script that you want to import, not the scripts themselves.
Click Select Folder.
In the list of files and folders, select the check boxes that correspond to the scripts or sub-folders that contain scripts that you want to import.
Click Next.
If you want imported objects to replace objects in the database project that have the same name and type, select the Overwrite objects that already exist in the project check box.
If you do not want to import extended properties, clear the Import extended properties check box.
If you want to import permissions, select the Import Permissions check box.
If you want to import permissions and add them to the project mode, you must also select the Add imported permissions to the project model script check box.
Note
If you import permissions and add them to the model, the amount of time required to open the database project might increase significantly.
In Encoding, click the encoding with which the script file was created.
Click Finish to import database object definitions from the script that you specified.
A progress page appears as the script is parsed and any object definitions are added to your database project. A log file is created when you import database definitions from a script. Log files are stored in the Import Script Logs subfolder within the project folder.
Note
The source file is not modified and opens in shared read-only mode.
To use the Visual Studio automation model
Either create or open a database project.
For more information, see How to: Create Empty Database and Server Projects or How to: Open a Database or Server Project.
Open the View menu, point to Other Windows, and click Command Window.
In the Command Window, type the following command:
Project.ImportScript /FileName "MyScript.sql"
Replace MyScript.sql with the name of the script that you want to import.
Note
You can specify additional options for the command that control the import operation. For more information, see Automation Command Reference for Database Features of Visual Studio. If you do not specify a file name, the Import Database Wizard appears.
The objects and settings are imported from the specified script into the project that was selected in Solution Explorer when you executed the command. The Error List window displays any errors that relate to the objects that are created when the script is imported.
At this point, you must configure your database project settings before you can build and deploy your database project. For more information, see How to: Configure Database Properties for Database Projects.
Issues and Limitations
The Import SQL Script File wizard searches the specified script for all Data Definition Language (DDL) CREATE statements and adds the corresponding objects to your projects. The following limitations apply:
Statements that are not recognized are put in the ScriptsIgnoredOnImport.sql file in the database project.
It is assumed that the script contains definitions for objects in a single database. All objects are imported into your current database project. If the script contains the definitions for multiple databases, the CREATE DATABASE and USE statements are ignored, and all objects are added to your database project. If you want to create multiple database projects, you must manually split the file so that you have one file for each database, and then import each file separately into their respective database projects. If you import a script that has multiple database definitions into a single database project, you might receive a significant number of errors.
ALTER DATABASE statements that add filegroups are also imported. If the script contains a CREATE statement that is followed by a DROP statement for the created object, the object will be imported.
Only ALTER TABLE statements that add constraints to a table or specify columns for a full-text index are imported. For constraints, only statements that add a single constraint are understood. Statements that add multiple constraints to a table are skipped.
Batches must be separated by GO statements.
Note
To import the result of running the script, rather than the CREATE statements that are contained in the script, you can run the script to create a database and then use the Import Database Schema command to import the resulting database.
See Also
Tasks
How to: Delete Database Objects