Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Oracle Advanced DML Alan Beaulieu Beaulieu Consulting July 2008
Agenda <ul><li>What is Advanced DML? </li></ul><ul><li>Conditional Inserts </li></ul><ul><li>Multi-table Inserts </li></ul...
What is Advanced DML? <ul><li>Non-standard ways to insert, update, delete </li></ul><ul><li>Includes variations on existin...
Inserts <ul><li>New (since 9i) variations allow you to:  </li></ul><ul><ul><li>Insert rows into one of several tables </li...
Insert First <ul><li>Allows each row to be placed into one of several tables using conditional logic. </li></ul><ul><li>Ex...
Insert All <ul><li>Allows each row to be placed into 0, 1, …N tables  using conditional logic. </li></ul><ul><li>Example: ...
Insert All (cntd) <ul><li>Allows different flavors of each row to be inserted into the same table. </li></ul><ul><li>Examp...
INSERT/UPDATE <ul><li>Pre-9i implementation of INSERT/UPDATE logic… </li></ul><ul><li>BEGIN </li></ul><ul><li>INSERT INTO ...
INSERT/UPDATE (cntd) <ul><li>New (9i) SQL command : MERGE </li></ul><ul><li>Allows you to both update and insert with a si...
Merge <ul><li>Allows data to be either updated or inserted using a single SQL statement. </li></ul><ul><li>Example: </li><...
Merge (cntd) <ul><li>Don’t need to specify both WHEN MATCHED and WHEN NOT MATCHED </li></ul><ul><li>Example: Fill in value...
Updating Using Joins <ul><li>SQL Server lets you do the following: </li></ul><ul><li>UPDATE Sales.SalesPerson  </li></ul><...
Updating Using Joins (cntd) <ul><li>Doesn’t work with Oracle unless you update through an inline view: </li></ul><ul><li>U...
Updating Using Joins (cntd) <ul><li>However, opposite statement doesn’t work: </li></ul><ul><li>UPDATE </li></ul><ul><li>(...
Deleting Using Joins <ul><li>Again, only through an inline view: </li></ul><ul><li>DELETE </li></ul><ul><li>(SELECT hdr.ss...
Upcoming SlideShare
Loading in …5
×

Oracle Advanced Dml

1,311 views

Published on

Published in: Technology
  • Be the first to comment

Oracle Advanced Dml

  1. 1. Oracle Advanced DML Alan Beaulieu Beaulieu Consulting July 2008
  2. 2. Agenda <ul><li>What is Advanced DML? </li></ul><ul><li>Conditional Inserts </li></ul><ul><li>Multi-table Inserts </li></ul><ul><li>Upserts (Update/Insert) </li></ul><ul><li>Updating via Joins </li></ul>
  3. 3. What is Advanced DML? <ul><li>Non-standard ways to insert, update, delete </li></ul><ul><li>Includes variations on existing statements along with new statements </li></ul><ul><li>Reduces need for procedural code (i.e. PL/SQL) </li></ul><ul><li>Especially pertinent to data-warehouse environments </li></ul>
  4. 4. Inserts <ul><li>New (since 9i) variations allow you to: </li></ul><ul><ul><li>Insert rows into one of several tables </li></ul></ul><ul><ul><li>Insert rows into multiple tables </li></ul></ul><ul><ul><li>Insert different flavors of the same row multiple times </li></ul></ul>
  5. 5. Insert First <ul><li>Allows each row to be placed into one of several tables using conditional logic. </li></ul><ul><li>Example: Insert into either current or history table depending on date </li></ul><ul><li>INSERT FIRST </li></ul><ul><li>WHEN cal_dt = trunc(sysdate) THEN INTO pos_sum_dly </li></ul><ul><li>VALUES (fund_id, cal_dt, security_id, …) </li></ul><ul><li>WHEN cal_dt < trunc(sysdate) THEN INTO pos_sum_dly_hist </li></ul><ul><li>VALUES (fund_id, cal_dt, security_id, …) </li></ul><ul><li>SELECT fund_id, cal_dt, asset_id security_id, … </li></ul><ul><li>FROM cus_pos_extrnl </li></ul>Insert today’s data into pos_sum_dly Insert old data into pos_sum_dly_hist
  6. 6. Insert All <ul><li>Allows each row to be placed into 0, 1, …N tables using conditional logic. </li></ul><ul><li>Example: Insert data into pos_sum_dly, along with optional warnings </li></ul><ul><li>INSERT ALL </li></ul><ul><li>WHEN mtrty_dt IS NULL THEN INTO di_exception_rpt </li></ul><ul><li>VALUES ( 'INFO:INVALID RSM_MATURITY_DATE in CUSPOS' , …) </li></ul><ul><li>WHEN 1 = 1 THEN INTO pos_sum_dly </li></ul><ul><li>VALUES (fund_id, cal_dt, security_id, …) </li></ul><ul><li>SELECT fund_id, cal_dt, asset_id security_id, … </li></ul><ul><li>FROM cus_pos_extrnl </li></ul>Insert warning record if maturity date is NULL Always insert into pos_sum_dly
  7. 7. Insert All (cntd) <ul><li>Allows different flavors of each row to be inserted into the same table. </li></ul><ul><li>Example: Add pricing data going out 7 days </li></ul><ul><li>INSERT ALL </li></ul><ul><li>INTO pricing_master </li></ul><ul><li>VALUES (sec_id, src_cd, prc_typ_cd, crncy_cd, effect_dt, …) </li></ul><ul><li>INTO pricing_master </li></ul><ul><li>VALUES (sec_id, src_cd, prc_typ_cd, crncy_cd, effect_dt + 1, …) </li></ul><ul><li>… </li></ul><ul><li>INTO pricing_master </li></ul><ul><li>VALUES (sec_id, src_cd, prc_typ_cd, crncy_cd, effect_dt + 6, …) </li></ul><ul><li>SELECT sec_id, src_cd, prc_typ_cd, crncy_cd, effect_dt, … </li></ul><ul><li>FROM pricing_extrnl </li></ul>
  8. 8. INSERT/UPDATE <ul><li>Pre-9i implementation of INSERT/UPDATE logic… </li></ul><ul><li>BEGIN </li></ul><ul><li>INSERT INTO table_x (a, b, c, …) </li></ul><ul><li>VALUES (v_1, v_2, v_3, …); </li></ul><ul><li>EXCEPTION </li></ul><ul><li>WHEN DUP_VAL_ON_INDEX </li></ul><ul><li>UPDATE table_x </li></ul><ul><li>SET b = v_2, c = v_3 </li></ul><ul><li>WHERE a = v_1; </li></ul><ul><li>END; </li></ul>
  9. 9. INSERT/UPDATE (cntd) <ul><li>New (9i) SQL command : MERGE </li></ul><ul><li>Allows you to both update and insert with a single statement </li></ul><ul><li>If a row already exists, data is updated; otherwise, data is inserted </li></ul><ul><li>Also includes the ability to do conditional DELETE’s </li></ul>
  10. 10. Merge <ul><li>Allows data to be either updated or inserted using a single SQL statement. </li></ul><ul><li>Example: </li></ul><ul><li>MERGE INTO pos_sum_dly trg USING </li></ul><ul><li>(SELECT fund_id, cal_dt, asset_id security_id, … </li></ul><ul><li>FROM cus_pos_extrnl) src </li></ul><ul><li>ON (trg.fund_id = src.fund_id, trg.cal_dt = src.cal_dt, …) </li></ul><ul><li>WHEN MATCHED THEN UPDATE </li></ul><ul><li>SET trg.mtrty_dt = src.pos_mtrty_dt, … </li></ul><ul><li>WHEN NOT MATCHED THEN INSERT </li></ul><ul><li>VALUES (src.fund_id, src.cal_dt, src.security_id, …) </li></ul>Row exists - Update No row exists - Insert
  11. 11. Merge (cntd) <ul><li>Don’t need to specify both WHEN MATCHED and WHEN NOT MATCHED </li></ul><ul><li>Example: Fill in values for denormalized column </li></ul><ul><li>MERGE /*+ PARALLEL(trg, 8) */ INTO income_recv_asof trg </li></ul><ul><li>USING </li></ul><ul><li>(SELECT /*+ FULL(ira) PARALLEL(ira, 8) USE_NL_WITH_INDEX(iraf, income_recv_asof_fin_key) */ </li></ul><ul><li>ira.ssb_fund, ira.period_indicator, ira.income_instance, </li></ul><ul><li>ira.asof_date, iraf.fin_currency </li></ul><ul><li>FROM income_recv_asof ira, </li></ul><ul><li>income_recv_asof_fin iraf </li></ul><ul><li>WHERE ira.ssb_fund = iraf.ssb_fund </li></ul><ul><li>AND ira.period_indicator = iraf.period_indicator </li></ul><ul><li>AND ira.income_instance = iraf.income_instance </li></ul><ul><li>AND ira.asof_date = iraf.asof_date </li></ul><ul><li>AND ira.income_currency <> iraf.fin_currency </li></ul><ul><li>) src </li></ul><ul><li>ON (src.ssb_fund = trg.ssb_fund </li></ul><ul><li>AND src.period_indicator = trg.period_indicator </li></ul><ul><li>AND src.income_instance = trg.income_instance </li></ul><ul><li>AND src.asof_date = trg.asof_date) </li></ul><ul><li>WHEN MATCHED THEN UPDATE SET trg.income_currency = src.fin_currency; </li></ul>Same table being updated…
  12. 12. Updating Using Joins <ul><li>SQL Server lets you do the following: </li></ul><ul><li>UPDATE Sales.SalesPerson </li></ul><ul><li>SET SalesYTD = SalesYTD + SubTotal </li></ul><ul><li>FROM Sales.SalesPerson AS sp </li></ul><ul><li>JOIN Sales.SalesOrderHeader AS so </li></ul><ul><li>ON sp.SalesPersonID = so.SalesPersonID </li></ul><ul><li>AND so.OrderDate = </li></ul><ul><li>(SELECT MAX(OrderDate) FROM Sales.SalesOrderHeader </li></ul><ul><li>WHERE SalesPersonID = sp.SalesPersonID); </li></ul>
  13. 13. Updating Using Joins (cntd) <ul><li>Doesn’t work with Oracle unless you update through an inline view: </li></ul><ul><li>UPDATE </li></ul><ul><li>(SELECT hdr.fund_type, dhr.fund_type new_typ </li></ul><ul><li>FROM fund_header hdr INNER JOIN dhr_t dhr </li></ul><ul><li>ON dhr.ssb_fund = hdr.ssb_fund </li></ul><ul><li>) </li></ul><ul><li>SET fund_type = new_typ; </li></ul>
  14. 14. Updating Using Joins (cntd) <ul><li>However, opposite statement doesn’t work: </li></ul><ul><li>UPDATE </li></ul><ul><li>(SELECT dhr.fund_type, hdr.fund_type new_typ </li></ul><ul><li>FROM dhr_t dhr INNER JOIN fund_header hdr </li></ul><ul><li>ON dhr.ssb_fund = hdr.ssb_fund AND hdr.eval_ind = '0' </li></ul><ul><li>) </li></ul><ul><li>SET fund_type = new_typ; </li></ul><ul><li>Error at line 1 </li></ul><ul><li>ORA-01779: cannot modify a column which maps to a non key-preserved table </li></ul>
  15. 15. Deleting Using Joins <ul><li>Again, only through an inline view: </li></ul><ul><li>DELETE </li></ul><ul><li>(SELECT hdr.ssb_fund </li></ul><ul><li>FROM fund_header hdr INNER JOIN dhr_t dhr </li></ul><ul><li>ON dhr.ssb_fund = hdr.ssb_fund </li></ul><ul><li>WHERE hdr.tot_mkt_value < 1 </li></ul><ul><li>AND dhr.max_bal < 100 </li></ul><ul><li>); </li></ul>Does it matter which columns are selected?

×