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 > DB2 Insert Into w/ select w/ joins

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-24-04, 16:52
stscrc stscrc is offline
Registered User
 
Join Date: Oct 2004
Posts: 13
DB2 Insert Into w/ select w/ joins

I'm trying to create a DB2 SQL stored proc that does an insert into like so...

insert into hh.mytbl (field1, field2)
select field1, field2
from hh.mytbl2
where somefield = 0

this syntax works fine, but as soon as I try to join tables in the from clause of the select criteria, the proc won't compile.

While I know that I could move the join criteria to the where clause I don't want to do that if I shouldn't have to (because of outer joins and such). And yes, the select statement works fine if I pull it out and run just that.

If someone can please help me get the following type of statement to work inside of a proc I would greatly appreciate it...

insert into hh.mytbl (field1, field2)
select t1.field1, t2.field2
from hh.mytbl2 t1
inner join hh.mytbl3 t2
on t1.field1 = t2.field2
where t1.somefield = 0
Reply With Quote
  #2 (permalink)  
Old 11-24-04, 20:24
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
I don't know why it doesn't work in the SP, but since inner join is the normal behavior if you just specify the join predicates in the where clause, why not just do that?

If you have a support contract with IBM, you should open a PMR.
__________________
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 11-26-04, 09:25
stscrc stscrc is offline
Registered User
 
Join Date: Oct 2004
Posts: 13
I love DB2

Found out the problem had nothing to do w/ the insert statement. I had comments like so in the proc...

/****************
some comment
****************/

when I changed them to the style below, things worked fine...

/* ***************
some comment
*************** */

... even though I just finished writing about 20 procs that compliled just fine w/ the first style of comment.
Reply With Quote
  #4 (permalink)  
Old 11-26-04, 09:54
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
I use -- to indicate comments on SQL procedures (the same as in an SQL Script). That is what IBM uses on their sample SP's.

I guess /* works because the SQL Procedures are converted to C programs.
__________________
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
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