CRUD Statements  «Prev  Next»
Lesson 3 Overview of updating data
Objective Describe how to update data with Transact-SQL.

Updating Data in SQL-Server

Updating data in SQL Server 2019 is a critical task, and Transact-SQL (T-SQL) provides a powerful and flexible set of commands to carry out this operation. The `UPDATE` statement is used to modify existing data in a table, and it can be precisely configured to update specific columns based on certain conditions.
  • Syntax for Updating Specific Columns
    UPDATE [table_name]
    SET [column_name1] = [value1], [column_name2] = [value2], ...
    WHERE [condition];
    
    • table_name: The name of the table containing the data you want to update.
    • column_name1, column_name2, ...: The names of the columns that you want to update.
    • value1, value2, ...: The new values that you want to set for the specified columns.
    • condition: A condition that specifies which rows should be updated. Only the rows that satisfy this condition will be updated.

Steps to Update Specific Columns in SQL Server 2019

  1. Connect to the Database: Use SQL Server Management Studio (SSMS) or any other preferred SQL client to connect to your SQL Server instance.
  2. Open a New Query Window: Once connected, open a new query window to write your T-SQL commands.
  3. Write the UPDATE Statement: In the query window, write your `UPDATE` statement, specifying the table name, the columns you want to update, their new values, and the condition to identify which rows should be updated.
  4. Execute the Statement: After writing the `UPDATE` statement, execute it. Make sure to review your statement carefully before executing to avoid updating unintended rows.
  5. Verify the Update: After executing the `UPDATE` statement, it is a good practice to verify that the data has been updated correctly. You can do this by executing a `SELECT` statement to retrieve the updated rows and inspect their values.

Example: Suppose you have a table named `Employees` with columns `EmployeeID`, `FirstName`, `LastName`, and `Salary`. To update the `Salary` of an employee with `EmployeeID` 101, you can use the following T-SQL command:
UPDATE Employees
SET Salary = 60000
WHERE EmployeeID = 101;

This statement updates the `Salary` column to 60000 for the employee with `EmployeeID` 101.
Updating specific columns in a table is a common and crucial operation in SQL Server. The `UPDATE` statement in Transact-SQL provides a robust way to perform these updates, ensuring that you can modify your data accurately and efficiently. Always ensure to include a specific `WHERE` clause to prevent unintentional updates to other rows in the table. Additionally, it is highly recommended to back up your data or test your update commands on a staging environment before executing them on a production database to ensure data integrity and consistency.
To update data in a table, you use the UPDATE Transact-SQL statement. When updating data, you must specify the names of the specific columns that you are going to update.
The UPDATE statement uses the following general syntax:

Increase an Integer value in SQL Server

To increase an integer value in SQL Server, you can use the `UPDATE` statement along with the `SET` clause. Here’s a basic example:
UPDATE table_name
SET column_name = column_name + 1
WHERE condition;

This statement increases the value in `column_name` by 1 for rows that match the specified `condition`. You can adjust the `+ 1` to increase the value by any desired amount.

The image highlights the `table_name` in the `UPDATE` statement in SQL Server. The `table_name` represents the name of the table where the update operation will take place. This is the table that will have its data modified based on the conditions and expressions specified in the `SET` and `WHERE` clauses of the statement.
1) The image highlights the `table_name` in the `UPDATE` statement in SQL Server. The `table_name` represents the name of the table where the update operation will take place. This is the table that will have its data modified based on the conditions and expressions specified in the `SET` and `WHERE` clauses of the statement.
UPDATE{ 
table_name WITH(<table_hint > [...n])
| view_name
| rowset_function
}

1) `table_name WITH (<table_hint>)`: Indicates which index to use. 2) Purpose: Overrides the query optimizer's index choice for improved control over the execution plan.
2) 1) `table_name WITH (<table_hint>)`: Indicates which index to use. 2) Purpose: Overrides the query optimizer's index choice for improved control over the execution plan.

The image highlights the `view_name` in the `UPDATE` statement for SQL Server. The `view_name` represents the name of the view being updated. This view must be updatable, meaning it directly references a base table that can be modified. When using a view in an `UPDATE` statement, any changes made to the view will also affect the underlying base table(s).
3) The image highlights the `view_name` in the `UPDATE` statement for SQL Server. The `view_name` represents the name of the view being updated. This view must be updatable, meaning it directly references a base table that can be modified. When using a view in an `UPDATE` statement, any changes made to the view will also affect the underlying base table(s).

1) `rowset_function`: Allows updating data retrieved from an external source using `OPENROWSET` or `OPENQUERY`.
2) Purpose: Useful for updating data based on external queries or data sets outside the current database.
4) 1) `rowset_function`: Allows updating data retrieved from an external source using `OPENROWSET` or `OPENQUERY`. 2) Purpose: Useful for updating data based on external queries or data sets outside the current database.

The image highlights the `column_name` in the `UPDATE` statement for SQL Server. The `column_name` represents the specific column in a table or view that you want to update. In the `SET` clause, this column is assigned a new value based on the provided expression, which can include constants, variables, functions, or even calculations.
5) The image highlights the `column_name` in the `UPDATE` statement for SQL Server. The `column_name` represents the specific column in a table or view that you want to update. In the `SET` clause, this column is assigned a new value based on the provided expression, which can include constants, variables, functions, or even calculations.

Expression: Can be any valid expression in SQL Server, including literals, functions, or calculations.
6) Expression: Can be any valid expression in SQL Server, including literals, functions, or calculations.

The image highlights the `@variable = expression` part of the `UPDATE` statement in SQL Server. This syntax assigns the value of the column (or an expression) to a declared variable before the update operation occurs. This is useful when you need to capture the existing value of the column for further processing or logging before the value is modified by the update operation.
7) The image highlights the `@variable = expression` part of the `UPDATE` statement in SQL Server. This syntax assigns the value of the column (or an expression) to a declared variable before the update operation occurs. This is useful when you need to capture the existing value of the column for further processing or logging before the value is modified by the update operation.

@variable = column = expression: Sets the declared variable @variable to the same value as the column being updated.
8) @variable = column = expression: Sets the declared variable @variable to the same value as the column being updated.

The image highlights the `FROM <table_source>` part of the `UPDATE` statement in SQL Server. The `table_source` refers to the name of the table(s), view(s), rowset function, or derived table from which the data is being updated. This allows you to specify the data source for the update operation, which can include complex queries involving joins, subqueries, or external data sources accessed through rowset functions like `OPENROWSET` or `OPENQUERY`.
9) The image highlights the `FROM ` part of the `UPDATE` statement in SQL Server. The `table_source` refers to the name of the table(s), view(s), rowset function, or derived table from which the data is being updated. This allows you to specify the data source for the update operation, which can include complex queries involving joins, subqueries, or external data sources accessed through rowset functions like `OPENROWSET` or `OPENQUERY`.

The search_condition consists of one or more conditions that filter the rows to be modified. Additionally, the search_condition can include join information if it's not specified in the FROM clause, allowing for more complex updates involving multiple tables.
10) The search_condition consists of one or more conditions that filter the rows to be modified. Additionally, the search_condition can include join information if it's not specified in the FROM clause, allowing for more complex updates involving multiple tables.

The image highlights the `WHERE CURRENT OF` clause in the SQL Server `UPDATE` statement, which is used to update the row currently being processed by a cursor. The `cursor_name | cursor_variable` refers to the name of a declared cursor or a cursor variable. This clause is useful in scenarios where you need to update the specific row that the cursor is currently positioned on, making it an essential tool in row-by-row processing with cursors.
11) The image highlights the `WHERE CURRENT OF` clause in the SQL Server `UPDATE` statement, which is used to update the row currently being processed by a cursor. The `cursor_name | cursor_variable` refers to the name of a declared cursor or a cursor variable. This clause is useful in scenarios where you need to update the specific row that the cursor is currently positioned on, making it an essential tool in row-by-row processing with cursors.

The image explains the `OPTION (<query_hint>)` clause in the `UPDATE` statement in SQL Server. The `query_hint` provides instructions to the SQL Server optimizer on how to process the query. By using specific query hints, you can influence the behavior of the query, such as forcing a certain join type, disabling parallelism, or controlling the execution plan used by the optimizer. This allows for fine-tuning the performance of the update operation.
12) The image explains the `OPTION ()` clause in the `UPDATE` statement in SQL Server. The `query_hint` provides instructions to the SQL Server optimizer on how to process the query. By using specific query hints, you can influence the behavior of the query, such as forcing a certain join type, disabling parallelism, or controlling the execution plan used by the optimizer. This allows for fine-tuning the performance of the update operation.



table_hint syntax
{ INDEX (index_val [,...n])
 | FASTFIRSTROW
 | HOLDLOCK  | PAGLOCK
 | READCOMMITTED
 | REPEATABLEREAD
 | ROWLOCK

query_hint syntax
query_hint syntax

IDENTITY Column: Unlike an INSERT statement, an UPDATE statement cannot update the values of an IDENTITY column. In the next lesson, you will learn about the DELETE statement.

SEMrush Software 3 SEMrush Banner 3