0

I have three tables - T1,T2 & T3.

For each row in T1 I need to fetch the all the data from that table and a few other columns from a third table T3 and insert into T2 //Which is partitioned version of T1

Do I need stored procedure for this?

sagi
  • 40,026
  • 6
  • 59
  • 84
Some Java Guy
  • 4,992
  • 19
  • 71
  • 108
  • 2
    How is the data being added? Is it a single row at a time, or is it a bulk upload of many rows? How is data inserted into T1 currently? Directly from Java/some other non-database code? – Boneist Mar 15 '16 at 09:25

1 Answers1

3

No you don't , it can be done with a simple insert as select:

INSERT INTO T2
SELECT t1.*,t3.col1,t3.col2...
FROM T1
LEFT OUTER JOIN t3
 ON(t1.ID? = t3.ID?)

Of course you have to change this query to whatever columns you want, and the join condition to the relations between the tables.

sagi
  • 40,026
  • 6
  • 59
  • 84
  • Thanks.. that helps a bit, have some issues here http://stackoverflow.com/questions/36008450/select-each-row-and-insert-into-another-table – Some Java Guy Mar 15 '16 at 10:21
  • Whilst you can do this using just SQL, I personally would code a stored procedure to handle the inserts into both T1 and T2. That way, should some other application come along that needs to do the same thing, you have a reusable module, and you don't have to replicate the logic in your other application. – Boneist Mar 15 '16 at 10:40