IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

how to insert into a table with another tables data


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
GANAPATH

New User


Joined: 06 Mar 2006
Posts: 35

PostPosted: Fri Jan 12, 2007 1:39 am
Reply with quote

Hi,

i have

TABLE A with COL1 ,COL2 and COL3
TABLE B with COL4, COL5 AND COL6

I WANT TO insert into TABLE C WITH values of

COL1,COL2,COL5

how can i do this.

Thanks,
Ganapath
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Fri Jan 12, 2007 2:08 am
Reply with quote

How does TABLE A and TABLE B relate to each other.
Back to top
View user's profile Send private message
RamsIBM

New User


Joined: 08 Jan 2007
Posts: 53
Location: Chennai

PostPosted: Fri Jan 12, 2007 1:28 pm
Reply with quote

INSERT INTO TABLEC SELECT X.COL1,X.COL2,Y.COL5 FROM TABLEA X,TABLEB Y WHERE X.<KEY FIELD> = Y.<KEY.FIELD>

Key Field is nothing relation field
Back to top
View user's profile Send private message
mkk157

Active User


Joined: 17 May 2006
Posts: 310

PostPosted: Fri Jan 12, 2007 3:14 pm
Reply with quote

Hi RamsIBM,



If there is a relation between the tables I agree with u. But Just imagine that there is no relation between the two tables. In that situation How can I write the query?
Back to top
View user's profile Send private message
RamsIBM

New User


Joined: 08 Jan 2007
Posts: 53
Location: Chennai

PostPosted: Fri Jan 12, 2007 4:23 pm
Reply with quote

Kranthi Kumar Maddineni,

Yeap, we have the way for that also..... icon_smile.gif

Let Ganapath come up with his requirement........
Back to top
View user's profile Send private message
GANAPATH

New User


Joined: 06 Mar 2006
Posts: 35

PostPosted: Fri Jan 12, 2007 6:47 pm
Reply with quote

Thanks for the answer.Its good to know different things, but none of my tables are related, all the columns that i am choosing to write to TABLE C are different Datatypes and the KEY cols of A and B are also of different datatypes.

Thanks in advance.

Ganapth
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Jan 13, 2007 1:15 am
Reply with quote

Hello,

Then it would most likely be easier to write COBOL code that reads a record from each table then writes the "new" data from the appropriate fields rather than trying to write a single sql statement that does it all.

Continue thru reading 1 record from each table and creating the new table until you have read all of the input (in one table (or the other) if they do not have the same number of rows).
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Sat Jan 13, 2007 4:12 am
Reply with quote

Ganapth,

The two tables MUST be related to each other in some way. Well, in fact they don?t, but you will get a cartesian product (the product of both tables). i.e. TABLE A has 2500 rows, TABLE B has 5500 rows, the cartesian product of these two table will be 13750000 rows. icon_eek.gif Even if you chose to use a COBOL program to populate TABLE C. How are you going to determine which row from TABLE B goes with which row from TABLE A?

If you can define this relationship, there is probable SQL to do it.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Jan 13, 2007 4:41 am
Reply with quote

Hello,

If code is written to move thru BOTH tables 1 row at a time, there will be no cartisian product.

If the application has a process that ALWAYS adds 1 row to each table their relation may be the order the rows were added - even though no identifier was defined so they could easily be joined. Row 2326 in table a was added at the same time row 2326 was added to table b and that is the relationship.

Let's say that the design of these tables may be questionable.

If you declare a cursor for each table and then fetch a row from "a" and a row from "b" you'll have one of each and can issue an inseert with the selected info. One thought is how will table c be defined - will it not relate to either of the others?

When this is working, what kind of data requirement will it support?
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Jan 13, 2007 4:42 am
Reply with quote

Hello,

Of course, if you actually want a cartisian product, that would be easy to arrange as Dave pointed out.
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Sat Jan 13, 2007 6:01 am
Reply with quote

Dick,

Even if they are related by row position, I just wanted Ganapth to recognize this relationship. It gives us something to work with. icon_biggrin.gif
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Store the data for fixed length COBOL Programming 1
No new posts Load new table with Old unload - DB2 DB2 6
No new posts Pulling a fixed number of records fro... DB2 2
No new posts Data set Rec-Cnt and Byte-Cnt Testing & Performance 2
No new posts SCOPE PENDING option -check data DB2 2
Search our Forums:

Back to Top