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 > default values vs formula

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-18-12, 14:17
rnem170 rnem170 is offline
Registered User
 
Join Date: Nov 2011
Posts: 9
default values vs formula

running: db2 luw 9.7

I have a general query on the setup of a column...

I have 3 columns (all varchar):

Type Model Description

Type and model are required, I would like Description not to be required, but by default be filled with Type concat Model, so, if I filled with

"9406" "520" "This is a 9406 model 520"
that would work..
but if I filled with
"9406" "520" it would fill the description with 9406520 automatically.

Is that possible? I can put a formula in, but then the DDL shows "GENERATED ALWAYS AS.....". I don't want it generated always.

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 01-18-12, 14:45
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
Did you try generated by default? Or you could use a trigger to populate if the value was null after insert and after update.
__________________
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
  #3 (permalink)  
Old 01-18-12, 16:38
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
There is no GENERATED BY DEFAULT for an expression. You will have to use BEFORE triggers to do it, not after.

Andy
Reply With Quote
  #4 (permalink)  
Old 01-18-12, 17:25
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
Excuse my ignorance about triggers, but why can't you use an AFTER trigger based on if the column has a null value?

Also, on the generated always, what about using the coalesce statement to populate the column with generated always? Would that work?
__________________
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
  #5 (permalink)  
Old 01-19-12, 09:19
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
After triggers are actions you take AFTER the data has been updated in the table, so how can you change the value of a column once it has been written? That is what BEFORE triggers are for.

I tried using the COALESCE idea. Unfortunately you cannot use the column that you are populating in the expression, so that will not work. It looks like the BEFORE trigger is the only way to go.

Andy
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