dbTalk Databases Forums  

Alter GENERATED ALWAYS Column in Table

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss Alter GENERATED ALWAYS Column in Table in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Fin
 
Posts: n/a

Default Alter GENERATED ALWAYS Column in Table - 08-12-2010 , 03:20 PM






I have a table defined say as follows with a generated always column :

CREATE TABLE STOCK_DATA (
STOCK_ID INTEGER NOT NULL WITH DEFAULT 0 ,
OPEN DECIMAL(7,2) NOT NULL ,
HIGH DECIMAL(7,2) NOT NULL ,
LOW DECIMAL(7,2) NOT NULL ,
CLOSE DECIMAL(7,2) NOT NULL ,
NEW_STOCK_ID INTEGER NOT NULL GENERATED ALWAYS AS
( CASE WHEN DEC(((CLOSE-OPEN)/OPEN)*100,5,2) >= 1 THEN STOCK_ID +
40000
WHEN DEC(((CLOSE-OPEN)/OPEN)*100,5,2) BETWEEN .01 AND .99 THEN
STOCK_ID + 10000
WHEN DEC(((CLOSE-OPEN)/OPEN)*100,5,2) <= -1 THEN STOCK_ID + 50000
WHEN DEC(((CLOSE-OPEN)/OPEN)*100,5,2) BETWEEN -.99 AND -.01 THEN
STOCK_ID + 20000
WHEN DEC(((CLOSE-OPEN)/OPEN)*100,5,2) = 0 THEN STOCK_ID + 30000
END ) )
COMPRESS YES
IN RAWDATA
INDEX IN INDEXES
NOT LOGGED INITIALLY ;

Now I need to change the calculated values for the BETWEEN clauses as
follows;
WHEN DEC(((CLOSE-OPEN)/OPEN)*100,5,2) BETWEEN .01 AND .99 THEN
STOCK_ID + 60000
WHEN DEC(((CLOSE-OPEN)/OPEN)*100,5,2) BETWEEN -.99 AND -.01 THEN
STOCK_ID + 70000

and have it recalculate the generated column for the entire table.

I've read it is possible but I'm unsure as the impact. I have many
MANY functions and Stored Procedures that reference this column, so is
it possible to do without dropping ?

Many thanks, Tim

Reply With Quote
  #2  
Old   
Fin
 
Posts: n/a

Default Re: Alter GENERATED ALWAYS Column in Table - 08-12-2010 , 04:45 PM






Update: I tried to alter as above, but it failed with a SQL0270N
reason code 21 which related means : A column cannot be dropped or
have its length, data type, or nullability altered on a table which is
a base table for a materialized query table.

Now I have several summary tables that reference this column, so if I
drop the summary tables that mean I can do this ?

Reply With Quote
  #3  
Old   
Serge Rielau
 
Posts: n/a

Default Re: Alter GENERATED ALWAYS Column in Table - 08-12-2010 , 09:45 PM



On 8/12/2010 5:45 PM, Fin wrote:
Quote:
Update: I tried to alter as above, but it failed with a SQL0270N
reason code 21 which related means : A column cannot be dropped or
have its length, data type, or nullability altered on a table which is
a base table for a materialized query table.

Now I have several summary tables that reference this column, so if I
drop the summary tables that mean I can do this ?
Yes, you can. You also need to put eeh table into SET INTEGRITY OFF
Then alter the expression and then SET INTEGRITY again with the FORCE
GENERATED options.

Cheers
Serge

--
Serge Rielau
SQL Architect DB2 for LUW
IBM Toronto Lab

Reply With Quote
  #4  
Old   
Fin
 
Posts: n/a

Default Re: Alter GENERATED ALWAYS Column in Table - 08-13-2010 , 09:48 AM



Thanks for the response Serge, thats a great relief.

Much appreciated.

Reply With Quote
  #5  
Old   
Fin
 
Posts: n/a

Default Re: Alter GENERATED ALWAYS Column in Table - 08-13-2010 , 11:06 AM



Just for future reference if others need to change a generated always
column attribute, you also need to add DROP EXPRESSION prior to the
SET GENERATE ALWAYS AS (Expression).

Otherwise it fails with an SQL0190N error.

ie:
SET INTEGRITY FOR x OFF;
ALTER TABLE x ALTER COLUMN y DROP EXPRESSION SET GENERATED ALWAYS AS
(expression).
SET INTEGRITY FOR x IMMEDIATE CHECKED FORCE GENERATED;

Thanks again Serge.

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.