Edit

Share via


Add database objects using item templates

Use the SQL Database Projects extension for Visual Studio Code to add common database objects to your project using item templates. Item templates generate properly formatted Transact-SQL (T-SQL) files with standard boilerplate code, helping you maintain consistency across your database schema.

Database objects added to a SQL project are validated during the project build. This validation helps you catch syntax and reference problems early, before deployment starts.

Prerequisites

  • Visual Studio Code installed
  • The SQL Database Projects extension
  • An existing SQL database project (.sqlproj)

Available item templates

The SQL Database Projects extension includes the following item templates:

Template Description
Schema Creates a new database schema for organizing database objects into logical groups.
Table-Valued Function Creates a function that returns a table result set.
Sequence Creates a sequence object that generates sequential numeric values.
Trigger Creates a DML trigger that runs when INSERT, UPDATE, or DELETE operations occur on a table or view.
Database Trigger Creates a DDL trigger that runs in response to database-level events.

Add an item to your project

Add database objects to your project using item templates from the Database Projects view.

  1. In the Database Projects view, right-click your SQL project or an included folder.

    Note

    To keep larger projects manageable, many SQL projects organize files by schema and object type. For example, you might use dbo/Tables or sales/Views.

  2. Select Add Item.

  3. Select the type of object you want to create.

  4. Enter a name for the new object.

The extension creates a T-SQL file in your project with template code for the selected object type.

Note

You can also add items using the Command Palette. Press Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS), and then type SQL Database Projects: Add Item.

Template examples

These examples show the T-SQL code generated by item templates.

Schema

The schema template creates a file with a CREATE SCHEMA statement:

Screenshot of generated schema file.

Table-valued function

The table-valued function template includes parameter and return table placeholders:

Screenshot of generated table-valued function.

Modify the parameter names, data types, and return table columns to match your requirements.