Skip to content

BSL258 — Using keyword "UNION" in queries

Summary

Using keyword "UNION" in queries

Identifiers

Field Value
Rule code BSL258
Compatible alias UnionAll
Severity WARNING
Enabled by default Yes
Implemented Yes
Tags query, performance

Behavior

  • The public identifier BSL258 and alias UnionAll are stable.
  • The rule reports the cases documented on this page.
  • Suppressions and project configuration are applied before publication.
  • The rule requires neither an external analyzer nor network access.

Configuration and suppression

BSL### is the primary stable identifier. The compatible alias is accepted in select, ignore, and compatible block suppression comments.

[tool.onec-hbk-bsl]
select = ["BSL258"]
ignore = ["UnionAll"]

All three suppression families support both a current line and a range. When an opening comment follows code, it affects only that line. Use any one form:

  • noqa:
Value = "example";  // noqa: BSL258
  • bsl-disable:
Value = "example";  // bsl-disable: BSL258
  • compatible BSLLS form:
Value = "example";  // BSLLS:UnionAll-off

When the same opening comment is on a line by itself, it starts a range. Close it with the matching marker from the same family:

// noqa: BSL258
// code without this diagnostic
// noqa-enable: BSL258

// bsl-disable: BSL258
// code without this diagnostic
// bsl-enable: BSL258

// BSLLS:UnionAll-off
// code without this diagnostic
// BSLLS:UnionAll-on

To disable the rule until the end of the file, omit the closing noqa-enable, bsl-enable, or BSLLS:…-on marker.

Opening and closing markers must belong to the same family.

Description

In most cases, when you need to combine the results of two or more queries into a single result set, employ UNION ALL clause instead of UNION. The recommendation is based on the algorithm of the UNION clause, which searches for and removes duplicates from the united result even when duplicates are impossible by the query design.

Employ UNION only when removing duplicates from the result is required.

Examples

Incorrect:

SELECT
GoodsReceipt.Ref
FROM
Document.GoodsReceipt AS GoodsReceipt

UNION

SELECT
GoodsSale.Ref
FROM
Document.GoodsSale AS GoodsSale

Correct:

SELECT
GoodsReceipt.Ref
FROM
Document.GoodsReceipt AS GoodsReceipt

UNION ALL

SELECT
GoodsSale.Ref
FROM
Document.GoodsSale AS GoodsSale

Sources