Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code vs SQL Formatting

I downloaded Visual Studio Code thinking it would be nice to write T-SQL scripts and have the pretty auto-formatting happen as write.

I've already installed the mssql extension.

When creating a new file, the file is a simple plaintext with no extension 'Untitled-1'.

How in the world do I tell VS Code this is a SQL script so it formats correctly?

If I hit Shift+ALT+F, I simply receive the error "There is no formatter for 'plaintext' installed."

If I save the file with the extension '.sql', the format is applied.

However, I don't want to have to save the file... I want it to just be a scratchpad.

Is there any way in VS Code to tell the editor the file should be formatted SQL other than saving the file as an SQL file?

Thanks!

like image 858
jchornsey Avatar asked Jan 18 '20 16:01

jchornsey


3 Answers

I hope this will be of some help to you.

If I understood you correctly, you can use first this shortcut:

Win: Ctrl+Shift+P / Mac: Cmd+Shift+p

And then type "Change Language Mode".

Also try and visit this link for additional info: https://marketplace.visualstudio.com/items?itemName=cymonk.sql-formatter

Also you can visit this link: https://code.visualstudio.com/docs/languages/overview where you can find additional info about that, for example:

Changing the language for the selected file

In VS Code, we default the language support for a file based on its filename extension. However, at times you may wish to change language modes, to do this click on the language indicator - which is located on the right hand of the Status Bar. This will bring up the Select Language Mode drop-down where you can select another language for the current file.

Tip: You can get the same drop-down by running the Change Language Mode command (Ctrl+K M).

like image 167
VBoka Avatar answered Oct 24 '22 09:10

VBoka


This is what I did to format queries.

  1. Install the SQLTools extension
  2. Open your query
  3. Press Ctrl+Shift+P
  4. Select "Format Document With"
  5. Select SQLTools

Done!

like image 22
KalC Avatar answered Oct 24 '22 11:10

KalC


The other methods work, but the easy button is to click on Plain Text on the status bar Click on Plain Text in status bar

and then type in "SQL" to use your favorite installed SQL extension

Set SQL

like image 3
TheRizza Avatar answered Oct 24 '22 10:10

TheRizza