DF112: ORDER BY is used in a view or a single statement (inline) table-valued function.

Last modified: December 25, 2024

The topic describes the DF112 T-SQL code analysis rule.

Category

EXECUTION RULES

Message

ORDER BY is used in a view or a single statement (inline) table-valued function.

Description

It is not recommended to use ORDER BY to sort the result set in a view or inline table-valued function, as it does not serve any meaningful purpose.

Additional information

Views and table-valued functions are typically used for data presentation rather than data storage. In many cases, the order in which data is presented does not affect its underlying meaning or integrity. Therefore, applying ORDER BY in these constructs may not provide significant benefits.

Noncompliant code example

CREATE OR ALTER VIEW dbo.DemoView
AS
SELECT TOP 100 PERCENT
  *
FROM master.dbo.spt_values sv
ORDER BY sv.number
GO

SELECT
  *
FROM dbo.DemoView dv

Compliant solution

CREATE OR ALTER VIEW dbo.DemoView
AS
SELECT TOP 100 PERCENT
  *
FROM master.dbo.spt_values sv
ORDER BY sv.number
GO

SELECT
  *
FROM dbo.DemoView dv

SELECT TOP 100 PERCENT
  *
FROM master.dbo.spt_values sv
ORDER BY sv.number