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 > Howto insert into table with only a generated value?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-17-10, 04:54
benny84 benny84 is offline
Registered User
 
Join Date: Apr 2010
Posts: 1
Howto insert into table with only a generated value?

Hello everyone, I have a little trick question ;-)

I have the following table:

Quote:
CREATE TABLE RoutingRoute (id INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL, PRIMARY KEY(id))
How do i insert into this table?

Quote:
INSERT INTO RoutingRoute (id) VALUES (null)
I get the following error message:

Quote:
PDOException: SQLSTATE[23502]: Not null violation: -407 [IBM][CLI Driver][DB2/LINUX] SQL0407N Assignment of a NULL value to a NOT NULL column "TBSPACEID=2, TABLEID=6, COLNO=0" is not allowed. SQLSTATE=23502
Reply With Quote
  #2 (permalink)  
Old 04-17-10, 08:18
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
Benny84, If you want DB2 to generate the value use VALUES(DEFAULT). If you want to supply the value (since you defined the column as GENERATED BY DEFAULT), use VALUES(integer-value) (ex. VALUES(2)).

VALUES(NULL) is giving you an error because you defined the column as NOT NULL.
Reply With Quote
  #3 (permalink)  
Old 04-17-10, 12:00
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
I see the problem if you have only one GENERATED BY DEFAULT AS IDENTITY column in the table to insert columns by values in this table.

Adding the dummy column in this table could make your life easier.

Kara
Reply With Quote
  #4 (permalink)  
Old 04-19-10, 07:49
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
No dummy column is needed. Just use the DEFAULT keyword for GENERATED columns. (It doesn't matter if it is a GENERATED ALWAYS or GENERATED BY DEFAULT column.)
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 04-19-10, 09:40
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by stolze View Post
No dummy column is needed. Just use the DEFAULT keyword for GENERATED columns. (It doesn't matter if it is a GENERATED ALWAYS or GENERATED BY DEFAULT column.)
Stealth_DBA already answered the question in a post above:
"Benny84, If you want DB2 to generate the value use VALUES(DEFAULT)."
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #6 (permalink)  
Old 04-19-10, 11:15
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Question Maybe ?

Quote:
Originally Posted by benny84 View Post
Hello everyone, I have a little trick question ;-)

I have the following table:



How do i insert into this table?



I get the following error message:
For me this one is not too bad:
Code:
insert into RoutingRoute
select ifnull(max(id), 0) + 1
  from RoutingRoute
Lenny
Reply With Quote
  #7 (permalink)  
Old 04-21-10, 12:13
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Thumbs up complete solution of the problem

You have to use DEFAULT instead of NULL:

Code:
INSERT INTO RoutingRoute (id) VALUES (DEFAULT); 
INSERT INTO RoutingRoute (id) VALUES (DEFAULT); 
INSERT INTO RoutingRoute (id) VALUES (DEFAULT); 

select * from RoutingRoute
Result:

Quote:
ID
1
2
3
Lenny
Reply With Quote
  #8 (permalink)  
Old 04-22-10, 08:50
karthik.try20 karthik.try20 is offline
Registered User
 
Join Date: Apr 2010
Location: Chennai
Posts: 4
Hi Lenny,

Nice solution

By
KK
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