On 9/29/05, Igor Neyman <ineyman@(protected)> wrote: > > From Oracle docs: > > "merge_insert_clause > ..................................... > ..................................... > You can specify this clause by itself or with the merge_update_clause. > If you specify both, then they can be in either order." > > > __ ____ ____ ____ ____ ____ ____ > > From: oracle-l-bounce@(protected) > [mailto:oracle-l-bounce@(protected)] On Behalf Of Ranko Mosic > Sent: Thursday, September 29, 2005 9:51 AM > To: Igor Neyman > Cc: Paul Drake; ORACLE-L > Subject: Re: Dimension table load - PLSQL question > > > Not in my test - I tried to omitt "matched" and it won't do. > SQL> l > 1 MERGE INTO bonuses D > 2 USING (SELECT employee_id, salary, department_id FROM employees > 3 WHERE department_id = 80) S > 4 ON (D.employee_id = S.employee_id) > 5 WHEN NOT MATCHED THEN INSERT (D.employee_id, D.bonus) > 6* VALUES (S.employee_id, S.salary*0.1) > SQL> / > VALUES (S.employee_id, S.salary*0.1) > * > ERROR at line 6: > ORA-00905 (See ORA-00905.ora-code.com): missing keyword > rm > > On 9/29/05, Igor Neyman <ineyman@(protected)> wrote: > > "when matched" clause is optional, so you should be fine without > updates. > As for returning PK, regular "INSERT" has "RETURNING" clause, > but I don't see one for "insert" used withing "MERGE". File > "enhancement request" with Oracle :) > > __ ____ ____ ____ ____ ____ ____ > > From: oracle-l-bounce@(protected) [mailto: > oracle-l-bounce@(protected) <mailto:oracle-l-bounce@(protected)> ] > On Behalf Of Ranko Mosic > Sent: Thursday, September 29, 2005 9:34 AM > To: Igor Neyman > Cc: Paul Drake; ORACLE-L > > Subject: Re: Dimension table load - PLSQL question > > > > What about returning key if record exists ? There is no > update happening. > > Thanks, rm. > > > On 9/29/05, Igor Neyman <ineyman@(protected) > <mailto:ineyman@(protected)> > wrote: > > Does it make a difference: 1 table or 6? > > MERGE INTO t > USING (SELECT(select descr1 from lkp_table1 where cd > = p_cd1) AS v_descr1, > select descr2 from > lkp_table2 where cd = p_cd2) AS v_descr2 , > .... etc. )from dual) c > ON (t.descr1 = c.v_descr1 and t.descr2 = c.v_descr2 > and ... etc) > WHEN NOT MATCHED INSERT (t.descr1, t.descr2, ...) > VALUES (c.v_descr1, c.v_descr2, ...) > > Really, no need to react the way, you did... > > Igor Neyman > > > __ ____ ____ ____ ____ ____ ____ > > From: oracle-l-bounce@(protected) [mailto: > oracle-l-bounce@(protected) <mailto:oracle-l-bounce@(protected)> ] > On Behalf Of Ranko Mosic > Sent: Thursday, September 29, 2005 8:45 AM > To: Paul Drake > Cc: ORACLE-L > Subject: Re: Dimension table load - PLSQL question > > > Thanks for very helpful, no patronizing answer. If > you've read more carefully what the problem is > you'd see that MERGE can't work because it works on one > table upserting another. > I have one table being inserted from 6 tables. > Thanks genius. > > > On 9/28/05, Paul Drake <bdbafh@(protected)> wrote: > > On 9/28/05, Ranko Mosic <ranko.mosic@(protected) > > wrote: > > Hi, > requirement: > - input parameters are codes p_cd1, > p_cd2, ... > - for these codes I get descriptions ( > select descr1 into v_descr1 from > > lkp_table1 where cd = p_cd1; select > descr2 into v_descr2 from lkp_table2 where > > cd = p_cd2 etc ) > - check if table t has records where > t.descr1 = v_descr1 > and t.descr2 = v_descr2 and on and on > ....; > - if row exists return primary key; > - if not then insert. > > What is the best way of doing it ( > simplest ) ? > > > Regards, Ranko. > > > > > Ranko, > > "Simplest way" is to solicit opinions without > using a search engine or checking the documentation. > Its also usually "simplest" to leverage the > existing provided functionality, rather than writing your own routines, > error handling, etc. > > A search of "oracle 10.1 upsert" in google.com <http://google.com> > <http://google.com/> + "I'm feeling lucky" produced this for me. > Perhaps you might get lucky too. > > Paul > > http://www.psoug.org/reference/merge.html > > > > MERGE <hint> INTO <table_name> > USING <table_view_or_query> > ON (<condition>) > WHEN MATCHED THEN <update_clause> > WHEN NOT MATCHED THEN <insert_clause>; > > > > > >
<div>That's for 10g. I am on 9.2 here. </div> <div>thanks, rm<br><br> </div> <div><span class="gmail_quote">On 9/29/05, <b class="gmail_sendername">Igor Neyman</b> <<a href="mailto:ineyman@(protected)">ineyman@(protected)< /a>> wrote:</span> <blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0 .8ex; BORDER-LEFT: #ccc 1px solid">From Oracle docs:<br><br>"merge_insert _clause<br>.....................................<br>............................ ......... <br>You can specify this clause by itself or with the merge_update_clause.<br >If you specify both, then they can be in either order."<br><br><br>__ ____ __ ____ ____ ____ ____ __<br><br>From: <a href="mailto:oracle-l-bounce@(protected) .org"> oracle-l-bounce@(protected)</a><br>[mailto:<a href="mailto:oracle-l-bounce @(protected)">oracle-l-bounce@(protected)</a>] On Behalf Of Ranko Mosic<br >Sent: Thursday, September 29, 2005 9:51 AM<br>To: Igor Neyman<br>Cc: Paul Drake ; ORACLE-L <br>Subject: Re: Dimension table load - PLSQL question<br><br><br>Not in my test - I tried to omitt "matched" and it won't do.<br>SQL> l<br>1 MERGE INTO bonuses D<br>2 USING (SELECT employee_id, salary, department_id FROM employees <br>3 WHERE department_id = 80) S<br>4 ON (D.employee_id = S.employee_id)<br>5 WHEN NOT MATCHED THEN INSERT (D.employee_id, D .bonus)<br>6* VALUES (S.employee_id, S.salary*0.1)<br>SQL> /<br>VALUES (S .employee_id, S.salary*0.1 )<br> *<br>ERROR at line 6:<br >ORA-00905 (See ORA-00905.ora-code.com): missing keyword<br>rm<br><br>On 9/29/05, Igor Neyman <<a href= "mailto:ineyman@(protected)">ineyman@(protected)</a>> wrote:<br><br> "when matched" clause is optional , so you should be fine without <br>updates.<br> As for returning PK, regular "INSERT" has "RETURNING" clause,<br>but I don't see one for "insert" used withing "MERGE". File<br> "enhancement request" with Oracle :) <br><br>__ ____ ____ ____ ____ ____ ____<br><br> From: <a href="mailto:oracle-l-bounce@(protected)">oracle-l-bounce @(protected)</a> [mailto:<br><a href="mailto:oracle-l-bounce@(protected)" >oracle-l-bounce@(protected) </a> <mailto:<a href="mailto:oracle-l-bounce@(protected)">oracle-l-bounce @(protected)</a>> ]<br>On Behalf Of Ranko Mosic<br> Sent: Thursday, September 29, 2005 9:34 AM<br> To: Igor Neyman<br> Cc: Paul Drake; ORACLE-L <br><br> Subject: Re: Dimension table load - PLSQL question<br><br><br><br> What about returning key if record exists ? There is no<br>update happening.<br><br> Thanks, rm.<br><br><br> On 9/29/05, Igor Neyman < <a href="mailto:ineyman@(protected)">ineyman@(protected)</a><br>< ;mailto:<a href="mailto:ineyman@(protected)">ineyman@(protected)</a>> > wrote:<br><br> Does it make a difference: 1 table or 6? <br><br>   ; MERGE INTO t<br> USING (SELECT (select descr1 from lkp_table1 where cd<br>= p_cd1) AS v_descr1,<br> select descr2 from<br>lkp_table2 where cd = p_cd2 ) AS v_descr2 , <br> .... etc. )from dual) c<br> ON (t.descr1 = c.v_descr1 and t.descr2 = c.v _descr2<br>and ... etc)<br>   ; WHEN NOT MATCHED INSERT (t.descr1, t.descr2, ...)<br> VALUES (c.v_descr1, c.v_descr2, ...)<br><br> Really, no need to react the way, you did...<br><br>   ; Igor Neyman<br><br><br>__ ____ ____ ____ ____ ____ ____ <br><br>   ; From: <a href="mailto:oracle-l-bounce@(protected)">oracle-l -bounce@(protected)</a> [mailto:<br><a href="mailto:oracle-l-bounce@(protected) .org">oracle-l-bounce@(protected)</a> <mailto:<a href="mailto:oracle-l -bounce@(protected)"> oracle-l-bounce@(protected)</a>> ]<br>On Behalf Of Ranko Mosic<br> Sent: Thursday, September 29, 2005 8:45 AM<br> To: Paul Drake<br> Cc: ORACLE-L<br> Subject: Re: Dimension table load - PLSQL question <br><br><br> Thanks for very helpful, no patronizing answer. If<br>you've read more carefully what the problem is<br> you'd see that MERGE can't work because it works on one<br>table upserting another. <br> I have one table being inserted from 6 tables.<br>   ; Thanks genius.<br><br><br> On 9/28/05, Paul Drake <<a href="mailto:bdbafh@(protected) .com">bdbafh@(protected)</a>> wrote:<br> <br> On 9/28/05, Ranko Mosic <<a href="mailto:ranko.mosic@(protected)">ranko.mosic@(protected)</a> > <br>wrote:<br><br> Hi,<br> requirement: <br> - input parameters are codes p_cd1,<br>p_cd2, ... <br> - for these codes I get descriptions (<br>select descr1 into v_descr1 from<br><br> lkp_table1 where cd = p_cd1; select <br>descr2 into v_descr2 from lkp_table2 where<br><br> cd = p_cd2 etc )<br>   ; - check if table t has records where<br>t.descr1 = v_descr1<br> and t.descr2 = v_descr2 and on and on<br>....;<br> - if row exists return primary key;<br> - if not then insert.<br><br> What is the best way of doing it ( <br>simplest ) ?<br><br><br> Regards, Ranko.<br><br> <br><br><br> Ranko,<br><br>   ; "Simplest way" is to solicit opinions without <br>using a search engine or checking the documentation.<br> Its also usually "simplest" to leverage the<br>existing provided functionality, rather than writing your own routines,<br>error handling, etc. <br><br>   ; A search of " ;oracle 10.1 upsert" in <a href="http://google.com">google.com</a><br>< <a href="http://google.com/">http://google.com/</a>> + "I'm feeling lucky" produced this for me. <br> Perhaps you might get lucky too.<br><br> Paul<br><br> <a href="http ://www.psoug.org/reference/merge.html">http://www.psoug.org/reference/merge.html </a> <br><br><br><br> MERGE <hint> INTO <table _name><br> USING < ;table_view_or_query><br> ON (<condition>)<br>   ; WHEN MATCHED THEN <update_clause> <br> WHEN NOT MATCHED THEN <insert_clause>;<br><br><br><br><br><br></blockquote></div><br>