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 an existing script. 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.

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.

To Import Database Object Definitions from a Script

To import database object definitions from a script

  1. 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.

  2. Click Next to advance to the Select File page after you read the introductory page of the wizard.

  3. In File name, type the file name, including the path, of the script that you want to import. You can also click the browse button to find the file.

  4. 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.

  5. In Encoding, click the encoding with which the script file was created.

  6. 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 sub-folder within the project folder.

    Note

    The source file is not modified and opens in shared read-only mode.

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: View Database Objects

How to: Delete Database Objects

Concepts

Scenario: Create and Modify Database and Server Objects

Terminology Overview of Database Edition