MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Auto Calculated Column

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Jul 01, 2005 9:57 am    Post subject: Auto Calculated Column Reply with quote

I need to create a table that will calculate the sum of col's 1 - 4 everytime a insert or a change is performed. Can I define the table in such a way this will happen? Trigger? Something else?

The table will look something like this:
Key
Col1 decimal
Col2 decimal
Col3 decimal
Col4 decimal
Total decimal - This will be the calculated col.
Back to top
View user's profile Send private message
bauer
Intermediate


Joined: 10 Oct 2003
Posts: 317
Topics: 50
Location: Germany

PostPosted: Mon Jul 04, 2005 12:52 am    Post subject: Reply with quote

Hi nascar9,

yes, trigger is a possible solution.

My understanding of your request is:

Total = COL1 + COL1 + COL3 + COL4 for each Row / for each key, not sum for complete column (s).

So try this:


Code:



CREATE TABLE   creator.TEST2
          ( KEY    CHAR(5)
           ,COL1   DECIMAL
           ,COL2   DECIMAL
           ,COL3   DECIMAL
           ,COL4   DECIMAL
           ,SUM    DECIMAL
         ) IN ... ;


CREATE TRIGGER  creator.TEST3
        AFTER INSERT
          ON creator.TEST2
          REFERENCING  NEW AS NEW_ROW
      FOR EACH ROW MODE DB2SQL
       UPDATE creator.TEST2
           SET SUM =
                 NEW_ROW.COL1
              +  NEW_ROW.COL2
              +  NEW_ROW.COL3
              +  NEW_ROW.COL4
           WHERE KEY = NEW_ROW.KEY ;




Create an additional trigger .... AFTER UPDATE .......

Some additional coding for NULL Values my be necessary, depends on your exact request.

regards,
bauer
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Tue Jul 05, 2005 10:10 am    Post subject: Reply with quote

bauer, Thanks for your reply. Your assumptions are correct, sum for each key. I was not sure if there was away to define a table to sum without a trigger. The SQL sever team tells me they have a column definition that will 'Auto Sum', just trying to find out if DB2 had an equivalent. BTW my mothers side of the family live in and around Munich.

Thanks
NASCAR9
Back to top
View user's profile Send private message
gupta_ssn
Beginner


Joined: 17 May 2005
Posts: 3
Topics: 1
Location: Randolph, NJ

PostPosted: Thu Jul 14, 2005 11:19 am    Post subject: Reply with quote

Hi nascar9,

u can do this without using a trigger. try this
CREATE TABLE creator.TEST2
( KEY CHAR(5)
,COL1 DECIMAL
,COL2 DECIMAL
,COL3 DECIMAL
,COL4 DECIMAL
,SUM DECIMAL GENERATED ALWAYS
AS (COL1 + COL2 + COL3 + COL4)
)

Regards,
_________________
Gupta
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Thu Jul 14, 2005 11:48 am    Post subject: Reply with quote

gupta_ssn,
I tried your suggestion. Not sure where the error is, parentheses look balanced to me. Here are the results:

CREATE TABLE HOURS.TESTAUTOSUM
(KEY INTEGER ,
COL1 DECIMAL(09,2) ,
COL2 DECIMAL(09,2) ,
COL3 DECIMAL(09,2) ,
COL4 DECIMAL(09,2) ,
TOT DECIMAL GENERATED ALWAYS
AS (COL1 + COL2 + COL3 + COL4)
)
IN FDBHRS.FTSTSTXM
;
---------+---------+---------+---------+---------+---------+---------+------
DSNT408I SQLCODE = -104, ERROR: ILLEGAL SYMBOL "(". SOME SYMBOLS THAT MIGHT
BE LEGAL ARE: LOCATOR SECURITY
DSNT418I SQLSTATE = 42601 SQLSTATE RETURN CODE
DSNT415I SQLERRP = DSNHPARS SQL PROCEDURE DETECTING ERROR
DSNT416I SQLERRD = 502 0 0 -1 605 0 SQL DIAGNOSTIC INFORMATION
DSNT416I SQLERRD = X'000001F6' X'00000000' X'00000000' X'FFFFFFFF'
X'0000025D' X'00000000' SQL DIAGNOSTIC INFORMATION
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
bauer
Intermediate


Joined: 10 Oct 2003
Posts: 317
Topics: 50
Location: Germany

PostPosted: Fri Jul 15, 2005 12:25 am    Post subject: Reply with quote

NASCAR9, gupta_ssn,

the GENERATED clause is only valid for ROWID datatype (or distinct data type based on ROWID data type).

This solution doesn't work.

regards,
bauer
Back to top
View user's profile Send private message
schintala
Beginner


Joined: 18 May 2005
Posts: 108
Topics: 31
Location: USA

PostPosted: Fri Jul 15, 2005 8:30 am    Post subject: Reply with quote

Gupta_ssn's SQL is true to create generated column in LUW applications. But it doesn't work in z/OS or OS/390 DB2. z/OS UDB requires rowid data type as bauer mentioned.
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Jul 15, 2005 9:34 am    Post subject: Reply with quote

bauer, schintala, Thanks for the clarification.
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group