Managing snippets
Published 18 May 2015
A SQL Prompt snippet is a block of code you can insert into your query. To insert a snippet, type the snippet name (eg ssf) into your query and press Enter or any other insertion key:
Snippet Manager
To see the list of snippets you can use, go to the SQL Prompt menu and select Snippet Manager. You can find specific snippets by searching for both name or description:
Creating a snippet
You can create a snippet from code you've typed into the query editor. Highlight the block of code, right-click and select Create Snippet.
The default snippet name is created automatically from the initial letters of the selected text. You can edit this if you want to use a different name.
You can use placeholders in your snippet code.
Creating a new snippet
- On the SQL Prompt menu, select Snippet Manager.
- Click New.
- In the Snippet box, type the text that will insert the snippet.
- Optionally, type a short description of your snippet in the Description box.
The description helps you to identify a snippet if you are unsure of the snippet name. You can leave the Description box blank if required. Type or paste the SQL code in the Code box.
In your snippet code you can specify:
- Click Save.
Using placeholders
You can use placeholders in your snippets to insert values into your code, change the caret position, define a selection, and more.
Default placeholders
To view the list of default placeholders, in the Code box, type $:
You can use the following placeholders in snippet code:
$CURSOR$ | Specifies the cursor position when the snippet is inserted. For example, |
$DATE$ | Inserts the current date. You can specify a custom date format, for example |
$DBNAME$ | Inserts the name of the connected database. |
$GUID$ | Inserts a globally unique identifier. |
$MACHINE$ | Inserts the name of machine running SQL Prompt. |
$PASTE$ | Inserts the contents of the clipboard. |
$SELECTEDTEXT$ | Inserts the selected text. You can use this placeholder to create a snippet that encloses your selection in a block, eg BEGIN...END. Snippets using this placeholder are included in the actions list . |
| Selects a block of the snippet text. For example, |
$SERVER$ | Inserts the name of the connected SQL server. |
$TIME$ | Inserts the current time. You can specify a custom time format, for example |
$USER$ | Inserts the name of the connected user. |
Creating a placeholder
To create a placeholder, in the Code box, surround the name of the placeholder with $, for example $myplaceholder$:
Use the Placeholders list to enter default values and change the order in which your placeholders are inserted:
Getting more snippets
Snippet repository on GitHub
If you're using Git, you can clone a repository of useful snippets contributed by Gaurav Vohra.
Thanks Gaurav!
tSQLt snippets for SQL Test
If you're using SQL Test, you can download a set of useful snippets from the tSQLt website (under Other Downloads).
The snippets include pre-defined code for:
- test creation and execution
- assertions
- isolating dependencies