rspec/rules/S4820/tsql/rule.adoc

84 lines
1.7 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
SQL Server can be tuned at ``++PROCEDURE++`` and ``++TRIGGER++`` levels thanks to several ``++SET++`` statements that change the current session handling of specific information.
This rule raises an issue when expected configuration is not set or is set with an unexpected value between the beginning of the ``++PROCEDURE++`` (or ``++TRIGGER++``) definition and the first statement that is not a ``++SET++``, ``++IF++`` or ``++DECLARE++``.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
When this rule is configured with ARITHABORT and ON.
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
CREATE PROCEDURE dbo.MyProc
AS
BEGIN
SET ARITHABORT OFF; -- Noncompliant; ARITHABORT is OFF
SELECT COUNT(*) FROM MY_TABLE
END;
----
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
ALTER PROCEDURE dbo.MyProc
AS
BEGIN
SELECT COUNT(*) FROM MY_TABLE
SET ARITHABORT ON; -- Noncompliant; ARITHABORT is not set at the beginning of the procedure definition
[...]
END;
----
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
CREATE PROCEDURE dbo.MyProc
AS
BEGIN
-- Noncompliant; ARITHABORT is not set at all, so default value of SQL Server will be applied
SELECT COUNT(*) FROM MY_TABLE
END;
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
CREATE PROCEDURE dbo.MyProc(@setConfig INT)
AS
BEGIN
IF @setConfig=1
BEGIN
SET ARITHABORT ON;
END
SELECT COUNT(*) FROM MY_TABLE
END;
----
2022-02-04 17:28:24 +01:00
[source,sql]
2021-04-28 16:49:39 +02:00
----
ALTER PROCEDURE dbo.MyProc
AS
BEGIN
DECLARE @var INT;
SET ARITHABORT ON;
[...]
END;
----
2021-04-28 16:49:39 +02:00
== See
* https://docs.microsoft.com/en-us/sql/t-sql/statements/set-statements-transact-sql?view=sql-server-2017[SET Statements] - SQL Server (Transact-SQL) Documentation
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::parameters.adoc[]
endif::env-github,rspecator-view[]