Usually in SQL Server
Common Table Expression clause there is semicolon in front of the statement, like this:
;WITH OrderedOrders AS --semicolon here ( SELECT SalesOrderID, OrderDate, ROW_NUMBER() OVER (ORDER BY OrderDate) AS 'RowNumber' FROM Sales.SalesOrderHeader ) SELECT * FROM OrderedOrders WHERE RowNumber BETWEEN 50 AND 60
Why?
Some database systems require a semicolon at the end of each SQL statement. Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.
If you're writing single statements in, say, PHP and then sending them to MySQL for processing, the semicolon is optional. You ask if it "might have possible negative effects maybe during server high load, caching etc." The answer to that is 'No'.
The semicolon (;) is used in SQL code as a statement terminator. For most SQL Server T-SQL statements it is not mandatory.
CTEs, like database views and derived tables, enable users to more easily write and maintain complex queries via increased readability and simplification. This reduction in complexity is achieved by deconstructing ordinarily complex queries into simple blocks to be used, and reused if necessary, in rewriting the query.
..FROM..WITH (NOLOCK)..
RESTORE..WITH MOVE..
;
in SQL ServerPut together, the previous statement must be terminated before a WITH/CTE. To avoid errors, most folk use ;WITH
because we don't know what is before the CTE
So
DECLARE @foo int; WITH OrderedOrders AS ( SELECT SalesOrderID, OrderDate, ...;
is the same as
DECLARE @foo int ;WITH OrderedOrders AS ( SELECT SalesOrderID, OrderDate, ...;
The MERGE command has a similar requirement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With