rspec/rules/S3964/tsql/rule.adoc

72 lines
1.7 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
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
2021-04-28 16:49:39 +02:00
In the code sample below, the exception will never be rethrown because ``++SELECT ERROR_MESSAGE() THROW++`` will be interpreted as a column alias.
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
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)
=== Message
Add a semicolon after this statement.
Remove this semicolon.
=== Highlighting
statement needing a semicolon added or removed
'''
== Comments And Links
(visible only on this page)
=== relates to: S1438
=== on 7 Apr 2017, 11:25:52 Pierre-Yves Nicolas wrote:
Not sure this should be a "bug detection" rule. That's the worst case but it's probably rare. The example code comes from \http://www.dbdelta.com/always-use-semicolon-statement-terminators/
endif::env-github,rspecator-view[]