Once I wrote a post on SQL Server Template Explorer that describes available TSQL templates and how they can be used. Just like templates, we have been given some ready-made Code Snippets that help us to construct the statements easily. Not only that, it allows us to add our own snippets using adding Code Snippet Manager.
First of all, let's see how we can use existing code snippets. Assume that you need to create a SQL Login and you cannot remember the syntax. What you can do is;
1. Either select Insert snippet... context menu in the Query Window or press Ctrl+K and Ctrl+X (Press Ctrl and hold, and then press K and X).
2. Select Login folder and then select Create SQL Authentication Login.
3. Change the code as you need.
Note that, like adding codes using Template Explorer, you do not get another interface for changing values. Values need to be manually changed.
If you need to add your own code snippet, you can take copy of an existing one, change as you want and save with your own name. Assume that you need to add Azure Firewall Setting as a code snippet. If so, here are the steps;
1. Open the SQL Server Code Snippet folder. If you have selected the default location when installing SQL Server, the path would be C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio\SQL\Snippets\1033.
2. Create a folder called Azure (or name as you need).
3. Update the SnippetsIndex.xml. Add the following node to the file. This node is for the newly created folder. (** Note that this file cannot be modified if you have not opened the editor as Administrator. If change this using Notepad, open the Notepad as an Administrator and then open the file for modifictions).
4. Take a copy of existing snippet and place in Azure folder. I have taken Create SQL Authentication Login.snippet and renamed as Create Azure Server Level Firewall Rule.
5. Open Create Azure Server Level Firewall Rule file (Open as an Administrator) and modify , and under with your details.
6. Modify node as per the snippet you need to add. In this example, the required code is EXEC sp_set_firewall_rule and it needs three parameters: Rule name, Starting parameter and Ending parameter. Parameters have to be added as node. Here is the way of adding this SP.
7. Done. Now the folder that contains the snippet has to be added to the Code Snippet Manager. Open Management Studio and select Code Snippet Manager menu item in the Tools menu.
8. Click on Add and add the Azure Folder (Or the folder you created).
9. Now the code snippet is available.
Friday, January 6, 2017
SQL Server - Adding IF, BEGIN/END, WHILE statement easily to the code
Do you know that you can add, specifically surround your code with BEGIN/END, IF, and WHILE statements using a menu item without writing them? Yes, with Management Studio, it is possible. Here are the steps for doing it;
Assume that you have the below code that needs to be repeated based on a condition;
All you have to do is, select the code needs to be surrounded and press Ctrl+K and Ctrl+S (Just press Ctrl and hold, and press K first and then S). This is what happen when you do it;
Select WHILE and press Tab key. This is what you should see;
Set the condition as you need now. This shortcut can be opened using the context menu as well.
Thursday, December 31, 2015
Query Shortcuts in Management Studio
The most friendly database tool, Management Studio was released with SQL Server 2005 and it has become the right-hand for both database administrators and developers. SQL Server Management Studio is an integrated environment for doing all types of operations and management related to SQL Server and it is a rich tool equipped with many functionalities and an editor. Although we have been using it for long time, Query Shortcuts are still unknown to many. Here is a post on it, and you will surely find the usefulness of them.
Query Shortcuts allows you to configure key combinations for executing your common commands. By default, three shortcuts are already configured and can be used immediately. Seeing configured shortcuts and new shortcut configuration can be done with Options interface which can be opened with Tools menu.
As you see, three shortcuts are already configured. If you press Ctrl+1 in Query Editor, sp_who procedure will be automatically executed and you will see the result of it.
In addition to that, we can configure our own short cuts. Here are some example;
"SELECT * FROM " is configured with Ctrl+5 and "EXEC" is configured with Ctrl+6. This allows us to highlight either a table name or stored procedure and get it either queried or executed using assigned shortcut. For example, if Person.Person is selected and Ctrl+5 is pressed, the statement SELECT * FROM Person.Person will executed.
Make sure you do not assign shortcuts for all operations. Operations like delete should not be configured because you can accidentally delete records once configured.
Subscribe to:
Posts (Atom)














