56 lines
1.3 KiB
Plaintext
56 lines
1.3 KiB
Plaintext
In Transact-SQL, the semicolon statement terminator is in most cases optional. Therefore many developers don't use semicolons. However, in some situations missing semicolons may yield insidious errors.
|
|
|
|
|
|
Semicolons are required by the ANSI standard, and Microsoft https://docs.microsoft.com/en-us/sql/t-sql/language-elements/transact-sql-syntax-conventions-transact-sql[recommends] the consistent usage of semicolons and might make semicolons mandatory in a future version of SQL Server. Also, semicolons make the code more portable.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
In the code sample below, the exception will never be rethrown because ``++SELECT ERROR_MESSAGE() THROW++`` will be interpreted as a column alias.
|
|
|
|
----
|
|
BEGIN TRY
|
|
BEGIN TRAN;
|
|
SELECT 1/0 AS AnException;
|
|
COMMIT;
|
|
END TRY
|
|
BEGIN CATCH
|
|
SELECT ERROR_MESSAGE() -- Noncompliant; no exception will be thrown
|
|
THROW
|
|
END CATCH
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
BEGIN TRY
|
|
BEGIN TRAN;
|
|
SELECT 1/0 AS AnException;
|
|
COMMIT;
|
|
END TRY
|
|
BEGIN CATCH
|
|
SELECT ERROR_MESSAGE();
|
|
THROW;
|
|
END CATCH
|
|
----
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|