How does the C keyword work in SQL?

How many times have they seen with and how many times has SQL Server asked what s before that

How does it work ;with ... ?

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

Why should this be ; ?

+7
source share
4 answers

A semicolon is used in SQL to complete a query. Putting this before such a query, just make sure that the database understands that the previous query has ended.

Initially, this was necessary after each request, since they were entered in turn, so the database had to know when to start the request. When the entire query is sent on one line, you only need a semicolon in the case where the SQL syntax is not enough to determine where the query ends. Since the with keyword has different meanings, it sometimes requires a semicolon in front of it to make sure that it is not part of the previous query.

+14
source

Using WITH for CTE requires the previous statement to end with ; . Using it at the beginning as it guarantees the correct syntax

So MERGE in SQL Server 2008

See this question: Invalid syntax next to 'with' keyword ... previous statement must be interrupted by semicolon

+5
source

It is best to terminate every SQL statement with a semicolon. SQL Server documents (for example, here ) suggest that it will be provided for in a future version, there really is no excuse for not getting into the habit now.

To answer the question: you see ;WITH... in Stackoverflow, because the one who answers is a sloppy encoder OR the user replies that the person asking the question is a sloppy encoder (and they claim to be the last one when it is first :) The definition of "sloppy coder" here is someone who uses a semicolon when they are forced to do this.

+2
source

Using WITH for common table expressions (CTE). They tried to get CTE to define it as the first statement (i.e., they cannot be associated with other parts of the request, therefore ;;)

+1
source

All Articles