TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

DB2 - SQL Update Statement


The UPDATE statement updates the values of specified columns in the rows of a table.

It is used to modify the existing records in a table.

UPDATE table_name
   SET column1 = value1, column2 = value2, ...
  WHERE condition;

Be careful when you updating a records in a table. Have you noticed the WHERE clause in the UPDATE statement? The WHERE clause specifies which records that should be updated. If you omit the WHERE clause, all records in the table will be updated.


DB2 Database:

Below is a selection from the "Product" table in the DB2 database.

ProductIDProductNameCategoryPriceQtyOnHandTotalValue
7001MouseAccessories75.00
7002Harddrive65.00201,300
7003KeyboardAccessories36.00331,118.00
7004RamComponents23.5016376.00
7005HondaBikes1,200
7006Pen7.451074.50
7007CddriveAccessories75.00
7008SpeakerAccessories75.00

Example 1: Updating single row

The following SQL statement updates the first ProductID (ProductID = 7001) with a new ProductName and QtyOnHand.

UPDATE Product
   SET ProductName = 'Pendrive', QtyOnHand = 30
 WHERE ProductID = 7001;
Result:
ProductIDProductNameCategoryPriceQtyOnHandTotalValue
7001PendriveAccessories75.0030
7002Harddrive65.00201,300
7003KeyboardAccessories36.00331,118.00
7004RamComponents23.5016376.00
7005HondaBikes1,200
7006Pen7.451074.50
7007CddriveAccessories75.00
7008SpeakerAccessories75.00

Example 2: Updating multiple rows

The following SQL statement will update the ProductName to 'Pendrive' for all records where Category is 'Accessories'.

UPDATE Product
   SET ProductName = 'Pendrive'
 WHERE Category = 'Accessories';
Result:
ProductIDProductNameCategoryPriceQtyOnHandTotalValue
7001PendriveAccessories75.00
7002Harddrive65.00201,300
7003PendriveAccessories36.00331,118.00
7004RamComponents23.5016376.00
7005HondaBikes1,200
7006Pen7.451074.50
7007PendriveAccessories75.00
7008PendriveAccessories75.00

Tips:

If you omit the WHERE clause, ALL records will be updated. let us see an example below,

The below SQL query is same as example 2, but we ommitted WHERE clause.

UPDATE Product
  SET ProductName = 'Pendrive'
Result:
ProductIDProductNameCategoryPriceQtyOnHandTotalValue
7001PendriveAccessories75.00
7002Pendrive65.00201,300
7003PendriveAccessories36.00331,118.00
7004PendriveComponents23.5016376.00
7005PendriveBikes1,200
7006Pendrive7.451074.50
7007PendriveAccessories75.00
7008PendriveAccessories75.00

ALL records will be updated with ProductName is 'Pendrive'. So be careful when updating records.



If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal