Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

SQL UPDATE Statement

by suresh

Let us see how to write the SQL Server UPDATE statement. The SQL UPDATE Statement is used to update the existing records with new data. The syntax of the SQL Server update statement is

-- SQL Server UPDATE Syntax
UPDATE [Table] 
SET [Column1] = [Value1], 
    [Column2] = [Value2],
    [ColumnN] = [ValueN]
WHERE Condition
  • Table: Table name to perform SQL update operations (updating records)
  • Column1…ColumnN: Please select the column names on which you want to update.
  • Value1…ValueN: Please specify the values that you want to update.
  • Condition: Here, we have to provide filters or conditions. If the condition is TRUE, then only the SQL Server update statement will update the records.

We are going to perform the different types of update operations on this table in SQL Server

SQL UPDATE Statement 1

SQL UPDATE Statement Example

We are going to update the Yearly Income of all the Employees in [SQL Update] Table.

-- Example for SQL Server Update Statement
UPDATE [SQL Update]
SET [YearlyIncome] = 100000

Above query will update the yearly income of all the Employees to 100000

SQL UPDATE Statement 2

Here, we haven’t specified WHERE condition. That’s why the SQL Server updated all the 15 records. It is the most critical approach in the real-time environment because if you forgot the where clause, you are ruing the client data

SQL UPDATE Statement to Update Single record

In this example, we update one single record.

-- Example for Update Query in SQL Server
UPDATE [SQL Update]
SET [YearlyIncome] = 250000
WHERE [EmpId] = 5

See, it has updated the [Yearly Income] with 250000 for the Employee whose ID value = 5

SQL UPDATE Statement 3

SQL UPDATE Multiple records

In this example, We are going to update multiple records into [SQL Update] Table.

-- Example for Update Query in SQL Server
UPDATE [SQL Update]
SET [YearlyIncome] = 123456,
    [Sales] = 4500
WHERE [Occupation] = 'Professional'

As you see, the update statement updated the [Yearly Income] with 123456 and sale as 4500 for all the records whose Occupation is Professional

SQL UPDATE Statement 4

UPDATE Statement to Update with New values

Suppose the government has increased some Tax values to 0.5% for all Goods and Services (or any particular Good). Then we can use this Update statement to update those values with a new one. OR you want to increase the prices of your goods then you can use it.

-- Example for Update Query in SQL Server
UPDATE [SQL Update]
SET [Sales] = [Sales] * 2
SQL UPDATE Statement 5

How to update from another table

In this SQL Server update statement example, we show how to update a table using records from another table. Here, we update the record in [SQL Update] Table with the new values in a [SQL Updates] Table.

-- Example for Update Query in SQL Server
UPDATE [SQL Update]
SET [YearlyIncome] = ( 
			SELECT [YearlyIncome] FROM [SQL Updates]
			WHERE [SQL Update].[FirstName] = [SQL Updates].[FirstName]
			AND 
			[SQL Update].[LastName] = [SQL Updates].[LastName])
SQL UPDATE Statement 7

Let me see the data

SQL UPDATE Statement 6

SQL Server Update Stored Procedure

In this example, we use the SQL Server Update Statement inside a Stored procedure. We update the Last Name as Tutorial Gateway, and Occupation as Admin for all the records present in the Update table whose Yearly Income is greater than or equal to 80000.

-- Example for SQL UPDATE Stored Procedure
USE [SQL Tutorial]
GO

IF OBJECT_ID ( 'sp_UpdateEmployees', 'P' ) IS NOT NULL   
    DROP PROCEDURE sp_UpdateEmployees;  
GO

CREATE PROCEDURE sp_UpdateEmployees
AS
BEGIN
	SET NOCOUNT ON;
	UPDATE [SQL Update] SET [LastName] = N'Tutorial Gateway',
	                         [Occupation] = N'Admin'
        WHERE [YearlyIncome] >= 80000
	 
END
GO
SQL UPDATE Statement 8

Let me use EXEC Command (Execute Command) to execute the Update stored procedure

-- Example for SQL UPDATE Stored Procedure
USE [SQL Tutorial]
GO

EXEC dbo.sp_UpdateEmployees

GO
SQL UPDATE Statement 9

Now, let us see whether the execution of the stored procedure updated the Last Name and Occupation or not

-- Sql Server update statement example
USE [SQL Tutorial]
GO
SELECT [EmpID], [FirstName], [LastName], [Education]
 ,[Occupation], [YearlyIncome], [Sales]
  FROM [SQL Update]
SQL UPDATE Statement 10

Update Statement from Management Studio

If you can access the Management Studio, use the Intellisense to generate the UPDATE Statement. To do so, right-click on the Table, and Select Script Table as -> Update To -> New Query Editor Window

SQL UPDATE Statement 11

Once you selected the Update To New Query Editor Window, it will generate the below-shown Sql Server update statement query. As you can see, it is displaying all the column names and the data type that it will accept. All you have to do is, replace <> with your required value, and change the where condition.

SQL UPDATE Statement 12

and the Code generated by the Management Studio is:

-- Update Statement in Sql Server example
USE [SQL Tutorial]
GO

UPDATE [dbo].[SQL Update]
   SET [FirstName] = <FirstName, nvarchar(255),>
      ,[LastName] = <LastName, nvarchar(255),>
      ,[Education] = <Education, nvarchar(255),>
      ,[Occupation] = <Occupation, nvarchar(255),>
      ,[YearlyIncome] = <YearlyIncome, float,>
      ,[Sales] = <Sales, float,>
 WHERE <Search Conditions,,>
GO

For the Update statement demonstration purpose, let me add 100000 to employees yearly income column.

-- SQL Server UPDATE Example
USE [SQL Tutorial]
GO

UPDATE [dbo].[SQL Update]
   SET [YearlyIncome] = [YearlyIncome] + 100000
GO
SQL UPDATE Statement 13

Let me show you the Updated information or records

SQL UPDATE Statement 14

Placed Under: SQL

  • Install SQL Server
  • Install SQL Management Studio
  • Uninstall Management Studio
  • Install AdventureWorks Database
  • SQL Management Studio Intro
  • Connect SQL with sqlcmd utility
  • SQL Attach Database
  • SQL Detach Database
  • SQL Restore Database
  • Restore Database using BAK
  • SQL Rename Database with Files
  • Get SQL Database Names
  • SQL Create Table
  • SQL Rename Table
  • SQL Alter Table
  • SQL Add Column
  • SQL Rename Column
  • Get SQL Table Names in a DB
  • Find SQL Table Dependencies
  • Rename SQL Table & Column
  • SQL Global & Local Temp Table
  • SQL Table Variable
  • SQL Derived Table
  • SQL DATALENGTH
  • SQL Data Types
  • DML, DDL, DCL & TCL Cmds
  • SQL Query Builder
  • SQL ALIAS
  • SQL SELECT Statement
  • SQL SELECT DISTINCT
  • SQL SELECT INTO Statement
  • SQL INSERT Statement
  • SQL INSERT INTO SELECT
  • SQL BULK INSERT or BCP
  • SQL UPDATE Statement
  • SQL UPDATE from SELECT
  • SQL DELETE Statement
  • SQL TRUNCATE Table
  • SQL CASE Statement
  • SQL MERGE Statement
  • SQL Subquery
  • SQL CTE
  • SQL PIVOT
  • SQL UNPIVOT
  • SQL Clauses Examples
  • SQL TOP Clause
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Referential Integrity
  • SQL Check Constraint
  • SQL Unique Constraint
  • SQL Default Constraint
  • SQL Clustered Index
  • SQL Non Clustered Index
  • SQL Filtered Indexes
  • SQL COALESCE Function
  • SQL IS NOT NULL
  • SQL IS NULL Function
  • SQL ISNULL
  • SQL JOINS
  • SQL CROSS JOIN
  • SQL FULL JOIN
  • SQL SELF JOIN
  • SQL Outer Joins
  • SQL Cross Join Vs Inner Join
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL AND & OR Operators
  • SQL Arithmetic Operators
  • SQL BETWEEN Operator
  • SQL Comparison Operators
  • SQL LIKE
  • SQL EXCEPT
  • SQL EXISTS Operator
  • SQL NOT EXISTS Operator
  • SQL INTERSECT
  • SQL IN Operator
  • SQL NOT IN Operator
  • SQL UNION
  • SQL UNION ALL
  • SQL IF ELSE
  • SQL ELSE IF
  • SQL WHILE LOOP
  • SQL Nested While Loop
  • SQL BREAK Statement
  • SQL CONTINUE Statement
  • SQL GOTO Statement
  • SQL IIF Function
  • SQL CHOOSE Function
  • SQL Change Data Capture
  • SQL Table Partitioning
  • SQL Table Partition using SSMS
  • SQL TRY CATCH
  • SQL VIEWS
  • SQL User Defined Functions
  • SQL Alter User Defined Functions
  • SQL Stored Procedure Intro
  • Useful System Stored Procedures
  • SQL SELECT Stored Procedure
  • SQL INSERT Stored Procedure
  • SQL UPDATE Stored Procedure
  • Stored Procedure Return Values
  • Stored Procedure Output Params
  • Stored Procedure Input Params
  • Insert SP result into Temp Table
  • SQL Triggers Introduction
  • SQL AFTER INSERT Triggers
  • SQL AFTER UPDATE Triggers
  • SQL AFTER DELETE Triggers
  • SQL INSTEAD OF INSERT
  • SQL INSTEAD OF UPDATE
  • SQL INSTEAD OF DELETE
  • SQL STATIC CURSOR
  • SQL DYNAMIC CURSOR
  • SQL FORWARD_ONLY Cursor
  • SQL FAST_FORWARD CURSOR
  • SQL KEYSET CURSOR
  • SQL TRANSACTIONS
  • SQL Nested Transactions
  • SQL ACID Properties
  • Create SQL Windows Login
  • Create SQL Server Login
  • SQL Server Login Error
  • Create SQL Server Roles
  • SQL Maintenance Plan
  • Backup SQL Database
  • SQL Ranking Functions Intro
  • SQL RANK Function
  • SQL PERCENT_RANK Function
  • SQL DENSE_RANK Function
  • SQL NTILE Function
  • SQL ROW_NUMBER
  • SQL Aggregate Functions
  • SQL Date Functions
  • SQL Mathematical Functions
  • SQL String Functions
  • SQL CAST Function
  • SQL TRY CAST
  • SQL CONVERT
  • SQL TRY CONVERT
  • SQL PARSE Function
  • SQL TRY_PARSE Function
  • SQL Calculate Running Total
  • SQL Find Nth Highest Salary
  • SQL Reverse String
  • SQL FOR XML PATH
  • SQL FOR XML AUTO
  • SQL FOR XML RAW

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy