Firstly let me start by stating that when I originally set out in an IT career I was heading down a developer path, and certainly had a number of developer type roles over the years, or found ways of continuing development projects while working in infrastructure roles..... probably why I have an interest in DevOps. So taking that into account it's no surprise that for my entire career I have always been comfortable working in code and not relying on GUIs. Even for all the years as a SQL DBA armed with SQL Management Studio (SSMS), yet I was always most comfortable working in TSQL rather than the wizards. Probably comes from the days of Enterprise Manager and Query Analyser (ahhhhh nostalgia). Now the MS Product Team has done a great job at improving the wizards in SSMS and making tasks as easy as they can be in the tools. I will also state that this post is by no means saying SSMS is dead because there are just some things where it is better positioned.
What I will cover in this blog post is why my go to TSQL editor and tool for general database work is now VSCode with the MSSQL extension.
Please don't take this as a statement that I have now uninstalled SSMS or Visual Studio with SQL Data Tools (SSDT) from my laptop, I wish, but I have always found those tool a bit bloated with memory consumption when all I want to do is connect to a database, run some queries, or make some basic changes. What I will show is why/how I now perform those tasks with VS Code, but for anything more in depth like designing SSIS packages or performance troubleshooting I still rely on the existing tools (for now).
Another factor that is driving this adoption of a text based editor is that a large amount of my work is now with Azure and other cloud solutions, and for the majority of the work you need to do it is largely console or script based.
Now that you know why I have arrived at this place, lets get into how I setup and use VS Code for this purpose. I look forward to healthy discussions with people around this because I am not a believer of the "one size fits all" approach to a tool set either so it is always great to hear what others use.
Setup and configure your environment
Here are the steps I use to setup my VSCode environment:
What I will cover in this blog post is why my go to TSQL editor and tool for general database work is now VSCode with the MSSQL extension.
Please don't take this as a statement that I have now uninstalled SSMS or Visual Studio with SQL Data Tools (SSDT) from my laptop, I wish, but I have always found those tool a bit bloated with memory consumption when all I want to do is connect to a database, run some queries, or make some basic changes. What I will show is why/how I now perform those tasks with VS Code, but for anything more in depth like designing SSIS packages or performance troubleshooting I still rely on the existing tools (for now).
Another factor that is driving this adoption of a text based editor is that a large amount of my work is now with Azure and other cloud solutions, and for the majority of the work you need to do it is largely console or script based.
Now that you know why I have arrived at this place, lets get into how I setup and use VS Code for this purpose. I look forward to healthy discussions with people around this because I am not a believer of the "one size fits all" approach to a tool set either so it is always great to hear what others use.
Setup and configure your environment
Here are the steps I use to setup my VSCode environment:
- Download and install VSCode https://code.visualstudio.com/download
- Open VS Code
- Press Ctrl+Shift+X (on windows)
- Alternatively use the View > Extensions menu item
- Locate and install the following extensions
- vscode-icons
- mssql
- powershell
- c#
- Configure the extensions
- From File > Preferences > File Icon Theme select "VSCode Icons"
This will ensure that any files you open and access have nicely displayed icons to make your experience easier. - Configure the environment settings
- From File > Preferences > Settings
- VS Code works in two setting modes, User and Workspace. User should be personal preferences and Workspace should be used for project specific settings that will ship with the repo.
User settings are stored in the file C:\Users\\AppData\Roaming\Code\User\settings.json but you shouldn't have to edit that manually as the VS Code window provides the best method for working with these files. - I don't change too many settings at this time from the default, but some to consider depending on your needs are:
mssql.splitPaneSelection = "current|next|end"
IntelliSense will help you complete the values if you need to see what is available. - Now you should be ready to start working inside VS Code. However, I recommend reading the release notes when new updates are made as the developer community is extremely active improving VS Code and there is always new and useful features being added.
While VSCode has a built-in integrated terminal, I like the cmder tool for my terminal use. If you aren't familiar with cmder check it out, very versatile, run multiple terminals and languages. Best of all a Quake mode.
For more info check out my post on it http://blog.matticus.net/2017/03/cmder-enhance-your-windows-terminal.html
Connecting to a database and executing SQL queries
There are many tricks and ways to work within VS Code but here is a simply walk through on the basics to get you started.
There are many tricks and ways to work within VS Code but here is a simply walk through on the basics to get you started.
- Open VS Code if you haven't already
- You do not need to open a folder or save files just to run queries but it could be beneficial. Think of a folder like a Project/Solution, but in a simplier (faster) format. This works great with Git and cross platform collaboration.
For the case of this walk through just create a new file (click New File on the welcome page) - Without saving the file, lets make sure we are in the right language mode.
Click the current language in the tray menu (e.g. Plain Text)
This will open the command palette with prompts to select the language. Either browse or type to find your language and select it.
Now the correct SQL language is shown in the tray menu
Now the color coding and formatting, along with IntelliSense, will be suitable for SQL Server development.
TIP: When you save a file then the language mode is automatically detected based on the file extention. - Press Ctrl+Shift+P to open the Command Palette
- Type "mssql" and select the mssql: Connect option or press Ctrl+Shift+C
TIP: Make sure your focus is in a file with the SQL language set and not any other areas of VSCode when you press Ctrl+Shift+C as otherwise it will open a console as per those keyboard shortcuts default. - Select an existing connection profile or select the Create Connection Profile to create a new one. So lets create one.
- Follow the wizard filling out your server/instance, database (optional), authentication etc.
Once you start to connect the status is shown in the tray menu
Any errors connecting will be shown with an overlay
Once connected VS Code will update intellisense dictionary and perform other operations set by the extension. - Now write your query in the file
- When ready you can execute the query in a few methods
Use the Command Palette and the MSSQL: Execute Query command.
Right click in the editor and select
or my favorite just simply press Ctrl+Shift+E - The query results tab will open. By default this opens in a new split window column, or the next one if you have multiples. The idea here is so you can see the query and result all in one window.
You can put the query results at the bottom of the screen which might be a more familiar view to those use to SSMS. To do this select the Toggle Editor Group Layout from the View menu, or press Alt+Shiftt+1.
Now the results are below the query you executed.
Alternatively you can also set the query results to display in the current split window column (e.g. new tab)
So as you can see you can customise where the results are displayed just like in SSMS.
Something to keep in mind is that a new result tab will open for every file you execute a query from, but if you re-run a query or a new query from the same file then it will use the existing results tab for that file. - Now just like the query editor in SSMS, it will either execute the entire file contents or what you have selected. So like in this example it will just execute the selected query and not the entire file contents.
This is why I like the keyboard shortcut Ctrl+Shift+E to execute queries because it becomes really quick to work from a file and execute different selected queries as desired.
Obviously some people will really miss Object Explorer to understand the schema's of databases they are not familiar with, but keep in mind VSCode is designed for developers and so typically you would have a folder that contains all the scripts for creating the database and therefore your schema to refer to, or you would be familiar with the schema. However, as we all know there are plenty of views you can easily query to get that data (because after all that is all Object Explorer does).
Happy SQL Scripting.
Registering your SQL Server connections
VS Code has a json based configuration system. SQL Connections can be saved in the User Settings file, think along the lines of "Registered Servers" in SSMS. I have already briefly touched on how to create a new profile when you connect. However here is how to register them ahead of time and manage existing connection profiles.
Keep in mind though, these connections are not unique to a project/solution/folder, they are unique to your user settings. So you make sure you give them meaningful names to easily identify which databases/projects they belong to.
- Press Ctrl+Shift+P to open the Command Palette
- Type "mssql" and select the MSSQL: Manage Connection Profiles option
- The Command Palette will then prompt you with some more options.
Create: This will walk you through creating a new profile via the Command Palette prompts
Edit: This will open the User Settings JSON file and allow you to manually edit the connection profiles. NOTE: Passwords can be saved in an encrypted form but are not stored in this file for security.
Remove: This will walk you through removing an existing profile via the Command Palette prompts
This is an example of the JSON configuration provided with the Edit option. - Once you have configured the profile you can then simply select it from the list provided under the MSSQL: Connect command.
References
VS Code official site https://code.visualstudio.com/
VS Code opensource repo https://github.com/Microsoft/vscode
VS Code opensource repo https://github.com/Microsoft/vscode
Legal Stuff: The contents of this blog is provided “as-is”. The information, opinions and views expressed are those of the author and do not necessarily state or reflect those of any other company with affiliation to the products discussed. This includes any URLs or Tools. The author does not accept any responsibility from the use of the information or tools mentioned within this blog, and recommends adequate evaluation against your own requirements to measure suitability.
No comments:
Post a Comment