Home
DELETE and UPDATE statements should contain WHERE clauses
Description
The DELETE and UPDATE statements should contain WHERE clauses in order to limit the scope of the changes to only the rows that meet the specified criteria. This is important to ensure that the changes are only applied to the intended rows and that no unintended changes are made. Without a WHERE clause, the entire table or set of records will be affected, which could have serious consequences. Therefore, it is important to always include a WHERE clause when using DELETE and UPDATE statements in order to limit the scope of the changes.
Key Benefits
- Ensuring Data Integrity: DELETE and UPDATE statements should contain WHERE clauses to ensure that only the intended rows are affected.
- Preventing Unintended Changes: DELETE and UPDATE statements should contain WHERE clauses to prevent unintended changes to other rows.
- Improving Performance: DELETE and UPDATE statements should contain WHERE clauses to improve query performance.
Non-compliant Code Example
DELETE FROM Production.ProductCostHistory; --Non compliant code (Where clause is missing)
UPDATE Person.Address --Non compliant code (Where clause is missing)
SET ModifiedDate = GETDATE();
Compliant Code Example
DELETE FROM Production.ProductCostHistory --Compliant code
Where costId = 152;
UPDATE Person.Address --Compliant code
SET ModifiedDate = GETDATE()
Where id = 190;