Skip to main content
Dave Mason - Mastodon Dave Mason - LinkedIn Dave Mason - GitHub Dave Mason - Counter Social

Transact-SQL Code Snippets

Dave Mason - Code Snippets

There are certain T-SQL statements whose syntax I have trouble remembering. When those situations arise, I might look up the syntax online; find the same type of object in SSMS, right-click it, script out the object, and use that as a starting point; or find one of my own scripts that has the syntax I'm looking for. Another option that I often overlook is T-SQL code snippets.

What is a code snippet? Here's the definition from the Microsoft documentation:

A Transact-SQL code snippet is a template containing the basic structure of a Transact-SQL statement or block. You can use snippets as a starting point when adding statements in the Database Engine Query Editor. You can insert the pre-defined snippets supplied with SQL Server, or create your own.

Here's what it looks like in SSMS:

  1. Right-click in an SSMS window and click "Insert Snippet..."
  2. A drop-down list of snippet categories appears. Double-click one of the category folders to see the snippets in that group.
  3. A drop-down list of code snippets appears. Hover your mouse over any of them to see the snippet definition tooltip. Double-click one to insert the code to the SSMS window.
  4. If the snippet has replacement points (highlighted in yellow), the TAB key will cycle through them allowing you to conveniently replace each with the values you want. If you rest your mouse pointer on a replacement point, a tooltip appears with a description.

Dave Mason - SSMS - Insert Snippet
Dave Mason - Snippet Categories
Dave Mason - Code Snippets
Dave Mason - Code Snippet Replacement Points

SSMS has pre-defined snippets for the following categories:

  1. Function
  2. Index
  3. Login
  4. Role
  5. Schema
  6. Stored Procedure
  7. Synonym
  8. Table
  9. Trigger
  10. User
  11. User Defined Data Type
  12. User Defined Table Type
  13. User Defined Type
  14. View

Check 'em out. And happy coding!

(Would you like to create your own, custom code snippets? Continue reading...)


Post comment