Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to syntax-highlight in-line SQL?

Often times I write some SQL like this:

string sql = @"
   -- Multi-line SQL
";

Without getting into a debate on whether this approach is good or bad, can someone tell me what is the best way to get this SQL to be highlighted inside Visual Studio?

One approach I can think of is to create separate .sql files and then consume that as a string here so that when editing the original SQL, Visual Studio recognizes that this is a SQL file and syntax-highlights it.

This seems to be a tedious approach to solve a simple problem. Is there a simpler solution?

like image 494
Legend Avatar asked Jun 13 '12 21:06

Legend


People also ask

Can you highlight code in SQL?

For ease of use, components of an SQL query are shown in different colors. There are certain SQLyog defaults, but you can setup your own highlight fonts and color scheme by using the Preferences dialog (Tools -> Preferences).


2 Answers

JetBrains just released ReSharper 2016.2 which has support for inline syntax highlight and intellisense for regular expression and html with code comments like /*language=html*/.

I find that an amazing feature, which (I believe) could quite easily be extended to support SQL and almost any other language.

I've added a feature request here: https://youtrack.jetbrains.com/issue/RSRP-460656

This doesn't answer your question, but I'm hoping they'll consider adding such a feature, as inline SQL is quite common in code (ie. working with Dapper).

like image 189
Siewers Avatar answered Oct 16 '22 03:10

Siewers


There is a Visual Studio extension for this (for VS 2010 and 2012). It does have the problems you would imagine, such as highlighting SQ: keywords in strings that don't contain SQL. Depending on the type of work you do, you might still prefer this to not having highlighting in SQL.

http://visualstudiogallery.msdn.microsoft.com/a3a662c6-28eb-4de9-9a29-d328b1ac3f6b

One thing I can recommend is changing the SQL keyword colors in Visual Studio to be variations on the normal string colors. This way, the highlighting is helpful when correct, but isn't distracting in cases where it is highlighting a word in a string that doesn't actually contain SQL.

like image 27
Adam Hamilton Avatar answered Oct 16 '22 03:10

Adam Hamilton