View previous topic :: View next topic
|
Author |
Message |
Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA
|
|
|
|
I have file with key and value for specific column.
I want to insert the records to table from file if not present
and update columns with value in file if key is present in table.
can it be done using db2 load utility or any other solution for such request? |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Read the file. Query the table. If found update. If not found insert.
Is there a value in the file for one column or all of the columns? If there is only a value for one column, you need to decide what values to use for the other columns unless they have been defined with a default value.
You should validate the value(s) in the file before updating or inserting any rows. |
|
Back to top |
|
|
Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA
|
|
|
|
Quote: |
Is there a value in the file for one column or all of the columns? |
file has all the columns.
as file has many records reading each record then querying table and then again do insert\update using program would take more time.
I was looking for any option of bulk load or update if available.. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
How would that validate the data before doing an insert/update?
Quote: |
as file has many records reading each record then querying table and then again do insert\update using program would take more time.
|
A very bad choice is the choice between ensuring the data is valid over possibly saving a bit of processing time. . . |
|
Back to top |
|
|
Bharath Bhat
Active User
Joined: 20 Mar 2008 Posts: 283 Location: chennai
|
|
|
|
You can use multi-insert if your version of DB2 supports it.
Otherwise, write the records to be inserted into another file. Then use load utility to insert them into the table. But the updates has to happen inside the program. |
|
Back to top |
|
|
Escapa
Senior Member
Joined: 16 Feb 2007 Posts: 1399 Location: IL, USA
|
|
|
|
Hi All,
Am stuck up with similar request again, so continueing this thread..
We are going to add new column in table as char 1, This will hold values either Y or N.
There are around 2 million older records in the table (at the time of adding new column) out of which, I want do update 1.8 as flag Y and remaining .2 as value N.
I have those keys in seperate files. This as this is one time activity. |
|
Back to top |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
answering to the old question : There is such a SQL verb as MERGE (single row and/or multirow).
for the new question:
Load those keys to a seperate worktable say tab2 with an index on PK
then do
update tab1 set newcol = 'Y'
update tab1 A set newcol = 'N' where exists(select 1 from tab2 B where A.pk1 = B.pk1)
Probably an unload/sort merge /load can do the same,
but how to sort merge adding a field 'Y' when matched, 'N' when not matched, that you have to ask in the sort forum. |
|
Back to top |
|
|
|