0

I have a doubt in sql server which can be very basic or stupid for someone but i have the curiosity to know that why use of semicolon is not necessary to terminate the statement in sql server, that means how it comes to know that the statement has to be terminated at the particular point.

It would be great if anyone can help me knowing this.Thanks in advance

  • 1
    https://stackoverflow.com/questions/710683/when-should-i-use-semicolons-in-sql-server – Cedersved Dec 12 '19 at 12:01
  • Does this answer your question? [When should I use semicolons in SQL Server?](https://stackoverflow.com/questions/710683/when-should-i-use-semicolons-in-sql-server) – Amira Bedhiafi Dec 12 '19 at 12:20
  • The accepted answer is pretty old there @Cedersved . `MERGE`, for example, requires the **previous** statement to be correctly terminated as well. The *real* answer, however, is that you should be terminating *every* statement properly, as not doing so is deprecated in T-SQL and so your SQL could (will) stop working in a future version of SQL Server. It also means that when you use a statement that does require the **previous** statement to be properly terminated, it won't error if you are already doing so. – Thom A Dec 12 '19 at 12:20
  • It knows this because it has a complicated parser. `SELECT 1 A SELECT 2 B SELECT 3 C` must be three statements because the `SELECT` keyword, when not escaped, must necessarily begin a new statement. As more and more constructs are added, this not only complicates the parser even further but can make existing code ambiguous, which is why they'd really like if it people started using semicolons everywhere, and why it's mandated for certain newer statements to avoid any ambiguity. Compare it to reading English without punctuation: difficult, doable, but ambiguous if you're not careful. – Jeroen Mostert Dec 12 '19 at 12:23

1 Answers1

1

Although not required in all cases, it is good practice to use them as the documentation states that they will be required in a future version, although it's been saying that for a long time, you might as well just use them.

T-SQL Syntax Conventions

Also, there are some instances where you do indeed need to terminate a statement with a semi-colon, such as directly preceding a CTE for example.

steoleary
  • 8,968
  • 2
  • 33
  • 47
  • yes i have an idea about it that in some cases we need to use semicolon like CTEs and upsert queries but i want to know how compiler gets to know that a particular statement has to be terminated now say for eg there is a basic query to select like "select * from abc where id>5", and another query is "select * from xyz" and we are not using if we do not use semicolon here how the compiler gets to know that these 2 are separate queries. i hope now i am able to make you understand what am i asking for. – Swapnil Sharma Dec 12 '19 at 13:07
  • 1
    @SwapnilSharma: It knows this by the same token I know that your first sentence ends at "terminated" and another one starts at "now", even though you didn't actually use a period -- that's the only parsing that makes sense using English grammar. For T-SQL it's simpler because `SELECT` is designated as a keyword. When not nested in a subquery or otherwise part of a bigger query (like an `INSERT`), it can be nothing else but be the start of a new query (and therefore also the end of the previous one). – Jeroen Mostert Dec 12 '19 at 13:24