Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the "With" keyword work in SQL?

So many times seen with and, so many times SQL Server ask that with has ; before it

How does ;with ... work??

;with coords(...) as (
SELECT * ...
)

Why must have ; before it?

like image 264
edgarmtze Avatar asked Mar 07 '11 05:03

edgarmtze


1 Answers

The semicolon is used in SQL to end a query. Putting it before a query like that is just to make sure that the database understands that any previous query has ended.

Originally it was required after each query as they were entered line by line, so the database had to know when to run the query. When the entire query is sent in a single string, you only need semicolons in the case where the SQL syntax is not enough to determine where a query ends. As the with keyword has different uses a semicolon is sometimes needed before it to make sure that it's not part of the previous query.

like image 173
Guffa Avatar answered Sep 20 '22 12:09

Guffa