Share via


An Overview of Templates for Database and Server Objects

When you add an item to a database project, the default definition for that item comes from a prototype called a project item template. You can customize existing templates. You might decide to customize existing templates if, for example, you wanted to add comments to all the templates that your team uses. If you find yourself creating similar objects, even in different databases, creating a custom template can help save work.

Working with Templates

The following topics contain information about project item templates and how you can use them to create your own database project items:

Note

When you export a project template or an item template from a database project, your custom template will appear in the Add New Item dialog box in the Visual Studio Templates section under My Templates. The template does not appear with the other database objects nor under Visual Studio install templates in the Add New Item dialog box.

Database Object Templates

You can find the templates for database objects in the ItemTemplates and Items folders, which are inside your installation folder for Visual Studio (such as D:\Program Files\Microsoft Visual Studio 10.0\VSTSDB\Extensions\SqlServer\ItemTemplates\). Each database schema provider has its own templates. For example, each version of SQL Server has a Table template.

Replaceable Parameters in Database Object Templates

You can use the following replaceable parameters within the .sql files in your database object templates:

  • $ChildObjectName$
    Contains the name of the child database object. Use this when you define templates for items such as indexes, where the object that you are defining has a parent-child relationship to another database object.

  • $ParentObjectName$
    Contains the name of the parent database object. Use this when you define templates for items such as indexes, where the object that you are defining has a child-parent relationship to another database object.

  • $rawname$
    Contains the unmodified name that the user gives to the object in the Add New Item dialog box. Use this when you define templates for objects such as users or roles.

  • $SchemaName$
    Contains the name of the database schema. Use this when you define templates for items such as indexes, where the object you are defining has a child-parent relationship to another database object. You should prefix the parent object name with the schema name. For example, you can specify: ALTER TABLE [$SchemaName$].[$ParentObjectName$] ADD CONSTRAINT [$ChildObjectName$] UNIQUE (column_1).

  • $SchemaQualifiedObjectName$
    Contains the schema-qualified object name. Use this, for example, in the create table script: CREATE TABLE $SchemaQualifiedObjectName$ (col1 INT NOT NULL).

See Also

Other Resources

Visual Studio Templates

Finding and Sharing Project and Item Templates

Advanced Template Procedures

Visual Studio Template Reference