0

What is the meaning of ; at the end of queries?

For example; what is the difference between:

select 1
select 2

and

select 1;
select 2
Alex K.
  • 171,639
  • 30
  • 264
  • 288
Corovei Andrei
  • 1,646
  • 6
  • 28
  • 42
  • 1
    Possible duplicate of http://stackoverflow.com/questions/710683/when-should-i-use-semicolons-in-sql-server – Jan Oct 31 '12 at 12:19

4 Answers4

3

"From a SQLServerCentral.Com article by Ken Powers:

The Semicolon

The semicolon character is a statement terminator. It is a part of the ANSI SQL-92 standard, but was never used within Transact-SQL. Indeed, it was possible to code T-SQL for years without ever encountering a semicolon.

Usage

There are two situations in which you must use the semicolon. The first situation is where you use a Common Table Expression (CTE), and the CTE is not the first statement in the batch. The second is where you issue a Service Broker statement and the Service Broker statement is not the first statement in the batch."

this is from When should I use semicolons in SQL Server? which might be what you are looking for

which is in turn "straight a cut and paste"

I think this might be stack overflows version of a joke.

edit: ; are also after merge statements

Community
  • 1
  • 1
Nathan Cooper
  • 6,262
  • 4
  • 36
  • 75
2

Using the ; is the correct way to end one statement and start another. If you do not use the ; you may get unexpected results

Rob S
  • 69
  • 1
  • 9
  • >>If you do not use the ; you may get unexpected results<< Can you give an example of such 'unexpected results'? Or you mean error when there is no semicolon before CTE or after MERGE? – Igor Borisenko Oct 31 '12 at 12:35
  • The answer provided by Remus covers what I meant, it is always better to include it even if you dont think its absolutely necessary. – Rob S Oct 31 '12 at 13:11
  • OK. I didn't know that, thank you. I just got used to not using semicolons at my current job, though first time it was weird. Though for two years I haven't met any problems with that. – Igor Borisenko Oct 31 '12 at 13:48
1

It is to denote the end of an Sql statement.

Joe G Joseph
  • 23,518
  • 5
  • 56
  • 58
1

Giving a list of situation that are 'required' is always prone to omission. There are other situations besides commont table expression and SSB which the parser cannot handle w/o semicolon terminating the previous statement, like THROW or WITH XMLNAMESPACES and the list is bound to grow.

The correct answer is: semicolon terminators are required and should always be put whenever writing new code. Period. Omitting them is tolerated, but is on the official deprecation list:

Deprecated feature: Transact-SQL Not ending Transact-SQL statements with a semicolon
Replacement: End Transact-SQL statements with a semicolon ( ; ).

Ignoring this deprecation warning is on your own peril.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569