LINQ Style SQL
What I’m proposing, would change the way we type our SQL scripts. I’m not suggesting we try to pass or force through a new SQL standard. I’m simply suggesting a new dynamic “view” of the SQL syntax, which would allow for a more natural support of intellisense. With today’s IDE’s this simply doesn’t work. Intellisense doesn’t know where to pull the “what” from. The “what” comes from the “where”, (the FROM clause).
The underlying SQL script itself, would still comply with the SQL standards. This “view”, would perform three tasks: 1) allow the end-user to type their SQL more like LINQ, 2) rearrange current SQL scripts into that same LINQ style formatting, and 3) to translate this new format into standard compliant SQL, before executing and/or saving, within SQL Server Management Studio (SSMS).
By LINQ style, I mean, where the “from” and “how” (on what and what type of joins are used) is defined first, with the “what” (which columns/entity attributes) last. The conditionals (the WHERE clause) and the standard grouping (the GROUP BY clause) would go between the “from/how” and the “what”.
I would like to be able to type this and have intellisense work normally.
FROM Order O
JOIN OrderLine OL
ON O.ID = OL.OrderID
JOIN Item I
ON OL.ItemID = I.ID
JOIN Customer C
ON OL.CustomerID = C.ID
SELECT
C.Name
,I.Descrption
,OL.Quantity
,OL.Price
,(OL.Quantity * OL.Price) “Cost”
,O.Number
The transformed SQL script would be:
SELECT
C.Name
,I.Descrption
,OL.Quantity
,OL.Price
,(OL.Quantity * OL.Price) “Cost”
,O.Number
FROM Order O
JOIN OrderLine OL
ON O.ID = OL.OrderID
JOIN Item I
ON OL.ItemID = I.ID
JOIN Customer C
ON OL.CustomerID = C.ID
What I’m thinking of, is a setting tied to a toggle button on the SQL Prompt toolbar, which turns on/off this new “view”. If the toggle is turned on, the valid syntax would be the LINQ style, allowing for a more natural use of intellisense, and translate before execution and/or saving; if off, SQL Server Management Studio (SSMS) would simply work as it does now. This “view” would simply be another layer (a translator) between the text editor and the execution and/or saving of the SQL script.
Additional, down the road, benefits:
I’ve also read a couple different feature request to translate SQL scripts into LINQ and/or Lamda style expressions. This new “view”, or translator layer, could allow for this. A Right-Click, save as or export option could be handled here.
-
Randel Bjorkquist commented
SQL Example Format Update (I didn't realize the forum engine manipulates whitespaces):
I would like to be able to type this and have intellisense work normally:
.
FROM Order O
.
..JOIN OrderLine OL
....ON O.ID = OL.OrderID
.
..JOIN Item I
....ON OL.ItemID = I.ID
.
..JOIN Customer C
....ON OL.CustomerID = C.ID
.
SELECT
...C.Name
..,I.Descrption
..,OL.Quantity
..,OL.Price
..,(OL.Quantity * OL.Price) “Cost”
..,O.NumberThe transformed SQL script would be:
.
SELECT
...C.Name
..,I.Descrption
..,OL.Quantity
..,OL.Price
..,(OL.Quantity * OL.Price) “Cost”
..,O.Number
.
FROM Order O
.
..JOIN OrderLine OL
....ON O.ID = OL.OrderID
.
..JOIN Item I
....ON OL.ItemID = I.ID
.
..JOIN Customer C
....ON OL.CustomerID = C.ID -
Joseph Shilo commented
I would recommend that this be an option that enables an invisible/implicit Select * before the From clause and more syntactic sugar to allow the Select statement to come at the end of the query following Order By.