Lesson 6 | Approaches to using literal values |
Objective |
Describe approaches using literal values in your UPDATE and DELETE statements. |
Approaches to using Literal Values(UPDATE DELETE statements)
When issuing UPDATE
and DELETE
statements, you will want to use varying degrees of caution, depending on each individual circumstance.
A cautious approach
If you are not sure how many rows your UPDATE
or DELETE
statement will impact, you can issue a SELECT
statement first, using the same criteria.
This will show you how many rows will be affected when you run your query. Your select statement would look like this:
SELECT COUNT(*)
FROM Employees
WHERE EmployeeID = 101
An aggressive approach
A more aggressive approach would be to first issue the
UPDATE
or
DELETE
statement, then check the value of the SQL Server global variable,
@@ROWCOUNT.
This is aggressive because the rows will already have been updated by the time the
@@ROWCOUNT
variable is updated.
If more records were impacted than you expected, you have to figure out how to revise them back to their original values.
A medium approach
An approach between these two would be to wrap your statement within a transaction and roll it back if @@ROWCOUNT
is something other than what you expect.
In the next lesson, you will learn about using punctuation with your statements.