Tuesday, August 9, 2016

Matrix/Crosstab SQL using PIVOT functionality


Sample SQL's 

Using Decode and aggregate functions.

SELECT item
     , inventory
     , product_family
  FROM ( SELECT msi.segment1 item
, MAX( DECODE(mcs.category_set_name, 'Inventory', mc.segment1))       inventory
, MAX( DECODE(mcs.category_set_name, 'Product Family', mc.segment1))   product_family
            FROM mtl_system_items_b msi
               , mtl_item_categories mic
               , mtl_categories mc
               , mtl_category_sets mcs
           WHERE msi.organization_id = mic.organization_id
             AND msi.inventory_item_id = mic.inventory_item_id
             AND msi.segment1 = :p_item
             AND msi.organization_id = 103
             AND mic.category_set_id = mcs.category_set_id
             AND mic.category_id = mc.category_id
             AND mc.structure_id = mcs.structure_id
        GROUP BY msi.segment1)
                       


SELECT *
  FROM (SELECT msi.segment1 item
             , mcs.category_set_name category_set
             , mc.segment1
          FROM mtl_system_items_b msi
             , mtl_item_categories mic
             , mtl_categories mc
             , mtl_category_sets mcs
         WHERE 1 = 1
           AND msi.inventory_item_id = mic.inventory_item_id
           AND msi.organization_id = mic.organization_id
           AND msi.segment1 = :p_item
           AND msi.organization_id = 103
           AND mic.category_set_id = mcs.category_set_id
           AND mic.category_id = mc.category_id
           AND mc.structure_id = mcs.structure_id) 
PIVOT XML (MAX( segment1) AS category  --<-- pivot_clause
FOR( category_set)   --<-- pivot_for_clause
IN (select category_set_name from mtl_category_sets)) 
--<-- pivot_in_clause
                                                   
                                                   
                                                   
SELECT *
  FROM (SELECT msi.segment1 item
             , mcs.category_set_name category_set
             , mc.segment1
          FROM mtl_system_items_b msi
             , mtl_item_categories mic
             , mtl_categories mc
             , mtl_category_sets mcs
         WHERE 1 = 1
           AND msi.inventory_item_id = mic.inventory_item_id
           AND msi.organization_id = mic.organization_id
           AND msi.segment1 = :p_item
           AND msi.organization_id = 103
           AND mic.category_set_id = mcs.category_set_id
           AND mic.category_id = mc.category_id
           AND mc.structure_id = mcs.structure_id) 
PIVOT (MAX( segment1) AS category  --<-- pivot_clause
FOR( category_set) --<-- pivot_for_clause
IN  ('Inventory', 'Series', 'Model')) --<-- pivot_in_clause

Monday, August 8, 2016

Template to Use SAVE EXCEPTIONS in COLLECTIONS (Oracle Pl/SQL table Types)

DECLARE -- Oracle9i and above!
  l_array   <array_type_declaration>;
  bulk_errors EXCEPTION;
   PRAGMA EXCEPTION_INIT ( bulk_errors, -24381 );
BEGIN   
   FORALL indx IN l_array.FIRST .. l_array.LAST
      SAVE EXCEPTIONS
      /*DML statement of choice*/
      |;
EXCEPTION
   WHEN bulk_errors
   THEN
      FOR indx IN 1 .. SQL%BULK_EXCEPTIONS.COUNT
      LOOP
         DBMS_OUTPUT.PUT_LINE (
            'Error ' || indx || ' occurred during ' ||
            'iteration ' || SQL%BULK_EXCEPTIONS(indx).ERROR_INDEX ||
            ' updating name to ' ||SQL%BULK_EXCEPTIONS(indx).ERROR_INDEX);
         DBMS_OUTPUT.PUT_LINE (
            'Oracle error is ' ||
            SQLERRM(-1 * SQL%BULK_EXCEPTIONS(indx).ERROR_CODE));
      END LOOP;  
END;
/


--- Other way 

DECLARE -- Oracle9i and above!
  l_array   <array_type_declaration>;
BEGIN    
   FORALL indx IN l_array.FIRST .. l_array.LAST
      SAVE EXCEPTIONS
      /*DML statement of choice*/
      |;
EXCEPTION
   WHEN OTHERS
   THEN 
               IF SQLCODE = -24381
               THEN
      FOR indx IN 1 .. SQL%BULK_EXCEPTIONS.COUNT
      LOOP
         DBMS_OUTPUT.PUT_LINE (
            'Error ' || indx || ' occurred during ' ||
            'iteration ' || SQL%BULK_EXCEPTIONS(indx).ERROR_INDEX ||
            ' updating name to ' ||SQL%BULK_EXCEPTIONS(indx).ERROR_INDEX);
         DBMS_OUTPUT.PUT_LINE (
            'Oracle error is ' ||
            SQLERRM(-1 * SQL%BULK_EXCEPTIONS(indx).ERROR_CODE));
      END LOOP;   
           ELSE
               RAISE;
           END IF;
END;
/

Sample Script to APPLY_HOLD using OE_ORDER_PUB ( R12 ) at header level

CREATE OR REPLACE PROCEDURE apply_hold( p_request_rec IN oe_order_pub.request_rec_type)
IS
 l_header_rec              oe_order_pub.header_rec_type;
 l_line_tbl                oe_order_pub.line_tbl_type;
 l_request_rec             oe_order_pub.request_rec_type;
 l_action_request_tbl      oe_order_pub.request_tbl_type;
 x_header_val_rec          oe_order_pub.header_val_rec_type;
 x_header_adj_tbl          oe_order_pub.header_adj_tbl_type;
 x_header_adj_val_tbl      oe_order_pub.header_adj_val_tbl_type;
 x_header_price_att_tbl    oe_order_pub.header_price_att_tbl_type;
 x_header_adj_att_tbl      oe_order_pub.header_adj_att_tbl_type;
 x_header_adj_assoc_tbl    oe_order_pub.header_adj_assoc_tbl_type;
 x_header_scredit_tbl      oe_order_pub.header_scredit_tbl_type;
 x_header_scredit_val_tbl  oe_order_pub.header_scredit_val_tbl_type;
 x_line_val_tbl            oe_order_pub.line_val_tbl_type;
 x_line_adj_tbl            oe_order_pub.line_adj_tbl_type;
 x_line_adj_val_tbl        oe_order_pub.line_adj_val_tbl_type;
 x_line_price_att_tbl      oe_order_pub.line_price_att_tbl_type;
 x_line_adj_att_tbl        oe_order_pub.line_adj_att_tbl_type;
 x_line_adj_assoc_tbl      oe_order_pub.line_adj_assoc_tbl_type;
 x_line_scredit_tbl        oe_order_pub.line_scredit_tbl_type;
 x_line_scredit_val_tbl    oe_order_pub.line_scredit_val_tbl_type;
 x_lot_serial_tbl          oe_order_pub.lot_serial_tbl_type;
 x_lot_serial_val_tbl      oe_order_pub.lot_serial_val_tbl_type;
 x_action_request_tbl      oe_order_pub.request_tbl_type;
 x_debug_file              VARCHAR2(100);
 x_return_msg              VARCHAR2(4000); 
 l_msg_index_out           NUMBER(10);
 l_msg_count               NUMBER;
 l_msg_data                VARCHAR2(250);
 l_return_status           VARCHAR2(1);

BEGIN
   DBMS_OUTPUT.enable( 1000000);
   fnd_global.apps_initialize(12247
                            , 52844
                            , 660); -- pass in user_id, 
      ----responsibility_id, and application_id
   mo_global.init( 'ONT');
   mo_global.set_policy_context('S'
                              , 'org_id');
oe_msg_pub.initialize;
l_request_rec.entity_id := p_request_rec.entity_id;--SO header_id
l_request_rec.entity_code:= p_request_rec.entity_code; 
-- oe_globals.g_entity_header;
l_request_rec.request_type := p_request_rec.request_type; 
-- oe_globals.g_apply_hold;
l_request_rec.param1   := p_request_rec.param1; -- Hold_id
l_request_rec.param2 := p_request_rec.param2; --'O'; 
-- indicator that it is an order hold
l_request_rec.param3:= p_request_rec.param3; -- header_id
l_action_request_tbl( 1)            := l_request_rec;
   -- CALL TO PROCESS ORDER
   oe_order_pub.process_order(
   p_api_version_number             => 1.0
 , p_init_msg_list                  => fnd_api.g_false
 , p_return_values                  => fnd_api.g_false
 , p_action_commit                  => fnd_api.g_false
 , x_return_status                  => l_return_status
 , x_msg_count                      => l_msg_count
 , x_msg_data                       => l_msg_data
 , p_header_rec                     => l_header_rec
 , p_line_tbl                       => l_line_tbl
 , p_action_request_tbl             => l_action_request_tbl
 --OUT PARAMETERS
 , x_header_rec                     => l_header_rec
 , x_header_val_rec                 => x_header_val_rec
 , x_header_adj_tbl                 => x_header_adj_tbl
 , x_header_adj_val_tbl             => x_header_adj_val_tbl
 , x_header_price_att_tbl           => x_header_price_att_tbl
 , x_header_adj_att_tbl             => x_header_adj_att_tbl
 , x_header_adj_assoc_tbl           => x_header_adj_assoc_tbl
 , x_header_scredit_tbl             => x_header_scredit_tbl
 , x_header_scredit_val_tbl         => x_header_scredit_val_tbl
 , x_line_tbl                       => l_line_tbl
 , x_line_val_tbl                   => x_line_val_tbl
 , x_line_adj_tbl                   => x_line_adj_tbl
 , x_line_adj_val_tbl               => x_line_adj_val_tbl
 , x_line_price_att_tbl             => x_line_price_att_tbl
 , x_line_adj_att_tbl               => x_line_adj_att_tbl
 , x_line_adj_assoc_tbl             => x_line_adj_assoc_tbl
 , x_line_scredit_tbl               => x_line_scredit_tbl
 , x_line_scredit_val_tbl           => x_line_scredit_val_tbl
 , x_lot_serial_tbl                 => x_lot_serial_tbl
 , x_lot_serial_val_tbl             => x_lot_serial_val_tbl
 , x_action_request_tbl             => x_action_request_tbl);

   IF l_return_status = fnd_api.g_ret_sts_success
   THEN
      DBMS_OUTPUT.put_line(   'Hold applied for header id       ===============> '|| TO_CHAR( l_request_rec.entity_id)
                  || ' successfully!!!');
   ELSE
      FOR i IN 1 .. l_msg_count
      LOOP
         oe_msg_pub.get(p_msg_index      => i
                      , p_encoded        => fnd_api.g_false
                      , p_data           => l_msg_data
                      , p_msg_index_out  => l_msg_index_out);

         IF x_return_msg IS NULL
         THEN
            x_return_msg              :=
                  l_msg_index_out
               || ':'
               || l_msg_data;
         ELSE
            x_return_msg              :=
                  x_return_msg
               || '/'
               || l_msg_index_out
               || ':'
               || l_msg_data;
         END IF;
      END LOOP;

      DBMS_OUTPUT.put_line(   'Hold Application failed with reason : ' || x_return_msg);
   END IF;
END;