Columns to be read with a SELECT statement should be clearly defined


    This SQL Server code rule states that when using a SELECT statement, the columns that are to be read should be explicitly defined. This helps to ensure that only the data that is required is read, and that the query is more efficient. Additionally, it ensures that the query is more readable, as the columns that are being read are clearly defined.

Key Benefits

  • Defined Columns: The SELECT statement will only read the columns that are clearly defined, ensuring accuracy and consistency of the data.
  • Efficiency: This rule helps to make sure that the SELECT statement runs as efficiently as possible, reducing the amount of time needed to query the data.
  • Data Integrity: By limiting the columns that are read, this rule helps to ensure data integrity, as only the necessary data is being accessed.


Non-compliant Code Example

SELECT *   --Non compliant code (Selecting all the column irrespective it is required or not)
FROM Production.Product  
ORDER BY ListPrice;

Compliant Code Example

SELECT Product.ProductID,   --Compliant code 
FROM Production.Product  
ORDER BY ListPrice;
Visual Expert 2024