Friday, December 2, 2011

Updating Table for Particular Records or Rows

Scenario :  We have to update the particular column of a table for some rows range. Using csv data file.


Address Table







add idadd1add2Street city
100sampsamp2testtstcity
101sampsamp2testtstcity
102sampsamp2testtstcity
103samp
testtstcity
104samp
testtstcity
105samp
testtstcity
106samp
testtstcity
107samp
testtstcity
108samp
testtstcity
109samp
testtstcity
110samp
testtstcity
111sampsamp2testtstcity







Task: we have to update the "Add2" coloumn, add2 data is in cvs file. The previous data in that table should not be disturbed.

Solution:   First we have to create temp table for addess.


CMD:  db2 create table AddressTemp like Address


It will create table as the same structure as Address.
Now load CVS data in address temp.

CMD: db2 import from C:\test.csv of del insert into AddressTemp (addId,add1,add2,street,city)


It will load the data in Addresstemp Table.
The Update command is :

CMD: db2 update Address set add2= (select add2 from AddressTemp where Address.addid=AddressTemp.addid) where add2 between 103 and 110


Data will be updated from the addresstemp table for add2 coloumn.


Address Table







add idadd1add2Street city
100sampsamp2testtstcity
101sampsamp2testtstcity
102sampsamp2testtstcity
103sampsamp2testtstcity
104sampsamp2testtstcity
105sampsamp2testtstcity
106sampsamp2testtstcity
107sampsamp2testtstcity
108sampsamp2testtstcity
109sampsamp2testtstcity
110sampsamp2testtstcity
111sampsamp2testtstcity







No comments:

Post a Comment