If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > how to inert values into generated always column

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-09-11, 09:52
super_mpk super_mpk is offline
Registered User
 
Join Date: May 2011
Posts: 15
how to inert values into generated always column

Hi all,

I have a table with generated always column, I want to insert data itno the table but I can't as one of the column is generated always.

I am trying to use OVERRIDING USER VALUE but it is giving syntax error, so please guide me how to use this cmd. It i very imp foe me.

I am using db2 version 8 and OS is Window.

Thanks..............
Reply With Quote
  #2 (permalink)  
Old 06-09-11, 10:21
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
You could use the LOAD utility with the generatedoverride modifier.
Reply With Quote
  #3 (permalink)  
Old 06-09-11, 10:26
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Did you specified a column list after table name in your INSERT statement?
Reply With Quote
  #4 (permalink)  
Old 06-09-11, 10:43
super_mpk super_mpk is offline
Registered User
 
Join Date: May 2011
Posts: 15
I have two table t1 and t2.
describe table t1

Column Type Type
name schema name Length Scale Nulls
------------------------------ --------- ------------------ -------- ----- ------
CHARCOL1 SYSIBM VARCHAR 5 0 Yes
ID SYSIBM INTEGER 4 0 Yes


and t2 table is

Column Type Type
name schema name Length Scale Nulls
------------------------------ --------- ------------------ -------- ----- ------
CHARCOL2 SYSIBM VARCHAR 5 0 Yes
ID SYSIBM INTEGER 4 0 No

the id column in t2 table is generated always...
I am trying the command

insert into t2 overriding user value select * from t1;
Reply With Quote
  #5 (permalink)  
Old 06-09-11, 10:52
super_mpk super_mpk is offline
Registered User
 
Join Date: May 2011
Posts: 15
Can I use import utility to insert.
Reply With Quote
  #6 (permalink)  
Old 06-09-11, 11:09
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You can solve the problem by changing the GENERATED ALWAYS to GENERATED BY DEFAULT.

Code:
alter table t2 alter column id set generated by default
Andy
Reply With Quote
  #7 (permalink)  
Old 06-09-11, 11:15
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Go with Andy's advice! And to add to it, Never use generated always. The generated by default will perform the same way, but it allows you to replace data that may have been deleted and retain the same number you had previously. It allows you to easily copy the data to put into a test system and retain the RI that may be based on that identity column, etc...

Dave
Reply With Quote
  #8 (permalink)  
Old 06-09-11, 11:25
super_mpk super_mpk is offline
Registered User
 
Join Date: May 2011
Posts: 15
Thanks all

I tried that , I altered table as generated by default but the data is not inserting sequentially so that I am trying this Overriding option............
Reply With Quote
  #9 (permalink)  
Old 06-09-11, 12:47
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
What do you mean with "not inserting sequentially"? A table has no order of its rows unless you use an ORDER BY clause. So you could sort on the generated column.

If you are referring to gaps in the generated values, you cannot avoid that anyway: a transaction could do an insert and then rollback. The generated value is used but doesn't occur in any row in the table after the rollback. (If you rely on there being no gaps, you should revisit your design - usually, it has a problem. I know that there are sometimes legal requirements that you must not have gaps. In that case, generated values are not the way to go.)
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #10 (permalink)  
Old 06-09-11, 19:38
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
The basic question is why did you designed tables such way?

Is it not enough to add charcol2 to t1 and specify GENERATED ALWAYS(or BY DEFAULT) for id in t1?

Even if you want to separate tables by some reasons(for example, to reduce lock contensions),
why didn't specify GENERATED ALWAYS(or BY DEFAULT) for id in t1 and insert to t2 from t1?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On