Total Pageviews

Friday, July 21, 2017

Global Temporary Tables in PL/SQL

Example of  Global Temporary Tables in PL/SQL



declare
type mycur is ref cursor;
l_gtname varchar2(30) default 'Global_table_' || userenv('sessionid');
l_cursor mycur;
l_ename varchar2(50);
l_cnt number := 0;
begin
execute immediate 'create global temporary table ' ||
l_gtname || ' on commit delete rows
as
select * from scott.emp where 1=0 ';
execute immediate 'insert into ' || l_gtname ||
' select * from scott.emp where rownum = 1';
open l_cursor for
'select ename from ' || l_gtname || ' order by ename';
loop
l_cnt := l_cnt+1;
fetch l_cursor into l_ename;
exit when l_cnt = 10; --l_cursor%notfound;
dbms_output.put_line( l_cnt||'-'||l_ename );
end loop;
close l_cursor;
dbms_output.put_line(l_gtname);
execute immediate 'drop table ' || l_gtname;
end;

Query to find the available responsibilities for given operating_unit


SELECT fr.responsibility_name
FROM fnd_responsibility_vl fr,
FND_APPLICATION_TL fat,
fnd_profile_options_vl fpo,
FND_PROFILE_OPTION_VALUES fpov
WHERE fat.application_id = fr.application_id
AND fpov.level_value = fr.responsibility_id(+)
AND fpov. profile_option_value =:p_operating_unit_id
AND fpov.profile_option_id = fpo.profile_option_id
AND fpo.user_profile_option_name like 'MO: Operating Unit';

Creating Locations Using API Oracle Apps

Using API creating Location in Oracle Apps from backend 


API :hr_location_api.create_location

declare
l_validate boolean := false
;l_effective_date date := to_date('01-JAN-2008','DD-MON-YYYY')
;l_location_code varchar2(20) := '1234'
;l_address_line_1 varchar2(50) := '#1234, XXXXX'
;l_address_line_2 varchar2(50) := 'Adarsh Nagar'
;l_address_line_3 varchar2(50) := 'Hyderabad'
;l_location_id number
;l_business_group_id number := null
;l_object_version_number number;
begin
hr_location_api.create_location
(p_validate => l_validate
,p_effective_date => l_effective_date
,p_location_code => l_location_code
,p_address_line_1 => l_address_line_1
,p_address_line_2 => l_address_line_2
,p_address_line_3 => l_address_line_3
,p_location_id => l_location_id
,p_business_group_id => l_business_group_id
,p_object_version_number => l_object_version_number
,p_description => 'Loaded by API'
,p_inactive_date => '01-JAN-2010'
,p_style =>'US_GLB'
);
dbms_output.put_line(l_location_id);
END;

How to convert from Rows to column using sql

Converting Rows to Column Using SQL


declare
TYPE c_refcur IS REF CURSOR;
l_str VARCHAR2(4000);
l_col VARCHAR2(4000);
c_ref c_refcur;
l_num number;
l_query varchar2(4000) :='select column_name from all_tab_columns where table_name like ''MTL_SYSTEM_ITEMS_B'' and rownum < 6 order by 1 desc' ;
l_dlmtr varchar2(10) := ',';
BEGIN

OPEN c_ref FOR l_query;
LOOP
FETCH c_ref INTO l_col;

l_str := l_str || l_dlmtr || l_col;

EXIT WHEN c_ref%NOTFOUND;

END LOOP;
CLOSE c_ref;
dbms_output.put_line( 'select '||SUBSTR(l_str,2)|| ' from MTL_SYSTEM_ITEMS_B');
EXCEPTION
WHEN OTHERS THEN
l_str := SQLERRM;
IF c_ref%ISOPEN THEN
CLOSE c_ref;
END IF;

END;
/


OUTPUT: select SECONDARY_UOM_CODE,EAM_ACT_SHUTDOWN_STATUS,EAM_ACT_NOTIFICATION_FLAG,DUAL_UOM_DEVIATION_HIGH,DUAL_UOM_CONTROL,DUAL_UOM_CONTROL from MTL_SYSTEM_ITEMS_B

To find the concurrent manager for a specific concurrent request


SELECT b.user_concurrent_queue_name
FROM fnd_concurrent_processes a,
fnd_concurrent_queues_vl b,
fnd_concurrent_requests c
WHERE a.concurrent_queue_id = b.concurrent_queue_id
AND a.concurrent_process_id = c.controlling_manager
AND c.request_id = :p_request_id

PL/SQL REF Cursor Example




DECLARE
   TYPE array_t IS TABLE OF NUMBER
      INDEX BY BINARY_INTEGER;

   r_array     SYS_REFCURSOR;
   rec_array   array_t;
   l_num       NUMBER := 5;
   l_num1      NUMBER := 3;
BEGIN
   OPEN r_array FOR
      'SELECT OBJECT_ID FROM ALL_OBJECTS WHERE ROWNUM < :P_NUM AND OBJECT_ID > :P_N'
      USING l_num, l_num1;

   FETCH r_array
   BULK COLLECT INTO rec_array;


   FOR i IN rec_array.FIRST .. rec_array.LAST
   LOOP
      DBMS_OUTPUT.put_line (rec_array (i));
   END LOOP;

   CLOSE r_array;
END;

Sql Query to extract HZ_Geographies data, Query brings Flag set for each Postal Code



SELECT a.geography_id,
       a.geography_element2,
       a.geography_element3_code,
       a.geography_name postal_code,
       a.geography_element1_code country,
       deli.geography_name delivery_flag,
       pri.geography_name priority_flag
  FROM apps.hz_geographies a,
       (SELECT geography_name,
               geography_element4_id,
               geography_element3,
               geography_element2,
               geography_element1
          FROM apps.hz_geographies
         WHERE 1 = 1 AND geography_type = 'DELIVERY AVAILABLE') deli,
       (SELECT geography_name,
               geography_element4_id,
               geography_element3,
               geography_element2,
               geography_element1
          FROM apps.hz_geographies
         WHERE 1 = 1 AND geography_type = 'PRIORITY AVAILABLE') pri
 WHERE     1 = 1
       AND deli.geography_element3(+) = a.geography_element3
       AND deli.geography_element2(+) = a.geography_element2
       AND deli.geography_element1(+) = a.geography_element1
       AND a.geography_id = deli.geography_element4_id(+)
       AND pri.geography_element3(+) = a.geography_element3
       AND pri.geography_element2(+) = a.geography_element2
       AND pri.geography_element1(+) = a.geography_element1
       AND a.geography_id = pri.geography_element4_id(+)
      -- AND a.GEOGRAPHY_NAME = '2601'--'5254'
       AND a.geography_type = 'POSTAL_CODE'

HSN Code in Item Master INDIA localization screen Oracle Apps

HSN Code insert script for INDIA localization screen Oracle Apps by Avinash .


Table Used :jai_item_templ_hdr , jai_item_templ_dtls , jai_reporting_associations
Created a staging table xxx_item_hns_codes and insert all HSN codes and Item information.

Below script is used to insert the data to avoid manually work for functional . 


DECLARE

CURSOR C1 is 
SELECT  a.inventory_item_id, a.segment1 ,a.organization_id , b.organization_code, C.hsn_code
  FROM mtl_system_items_b a , mtl_parameters  b , xxx.xxx_item_hns_codes C
  WHERE a.organization_id  = b.organization_id
   and C.inventory_item_id is not null 
   and b.master_organization_id = <Give the organizatioin ID>
   --and  a.organization_id  = C.organization_id
    AND A.inventory_item_id = C.inventory_item_id ;
;
CURSOR C2 is 
 SELECT  lookup_code attribute_code
  FROM ja_lookups 
  WHERE lookup_type = 'JAI_ITEM_ATTRIBUTE'
   and lookup_code <> 'ITEM_CLASS'
   AND lookup_code NOT IN ('APPLICABLE','TRADABLE') ;

  l_template_hdr_id NUMBER ;
  l_count NUMBER ;
  l_count_det  NUMBER ;
  l_report_association_id NUMBER ;
  l_template_dtl_id NUMBER ;
  l_template_hdr_id_value NUMBER ;
  l_count_rep NUMBER ;
  l_jai_reporting_value NUMBER ;
  l_report_code_id NUMBER ;
BEGIN
      
  FOR I in C1 loop 
  l_template_hdr_id := NULL ;
  l_count := NULl ;
  l_template_hdr_id_value := NULL ;   
  l_count_det := NULL ;
  l_report_code_id := NULL ;
  l_jai_reporting_value := NULL ;
  
    SELECT jai_item_templ_hdr_s.nextval
 INTO l_template_hdr_id
      FROM   dual;
 
 SELECT count(1)
INTO l_count 
FROM  jai_item_templ_hdr
WHERE inventory_item_id = i.inventory_item_id
 AND organization_id =  i.organization_id
 AND entity_type_code IN ( 'ITEM' , 'ITEM_TEMPL_ASGN' )
      AND item_classification = 'PRFG' ;

  IF l_count = 0 THEN 
   
 BEGIN
 INSERT INTO jai_item_templ_hdr(  template_hdr_id
                                    ,template_name
                                    ,template_desc
                                    ,entity_id
                                    ,entity_type_code
                                    ,item_classification
                                    ,inventory_item_id
                                    ,organization_id
                                    ,creation_date
                                    ,created_by
                                    ,last_update_date
                                    ,last_update_login
                                    ,last_updated_by
                                    ,record_type_code)
    VALUES(l_template_hdr_id
          ,''
          ,''
          ,308308 --10080  -- 308308
          ,'ITEM' --    'ITEM_TEMPL_ASGN'  -- ITEM
          ,'PRFG'
          ,i.inventory_item_id
          ,i.organization_id
          ,SYSDATE 
          ,5562  --fnd_global.user_id
          ,SYSDATE 
          ,-1 --FND_GLOBAL.LOGIN_ID 
          ,5562 --FND_GLOBAL.USER_ID
          ,'DEFINED');
     EXCEPTION 
     WHEN OTHERS THEN
     DBMS_OUTPUT.PUT_LINE('Error while inserting into table jai_item_templ_hdr -- '|| sqlerrm);
     END ;    


    END IF ; 
COMMIT ;
BEGIN 
SELECT template_hdr_id
INTO l_template_hdr_id_value 
FROM  jai_item_templ_hdr
WHERE inventory_item_id = i.inventory_item_id
 AND organization_id =  i.organization_id
 AND entity_type_code IN (  'ITEM_TEMPL_ASGN', 'ITEM' )
      AND item_classification = 'PRFG' ;
EXCEPTION 
  WHEN OTHERS THEN
  l_template_hdr_id_value := NULL ;
  END  ;
 IF l_template_hdr_id_value IS NOT NULL THEN  
SELECT count(1)
INTO l_count_det 
FROM  jai_item_templ_dtls
WHERE template_hdr_id = l_template_hdr_id_value  ;
IF l_count_det = 0 THEN 
BEGIN  
 FOR k in C2 LOOP 
  
  SELECT jai_item_templ_dtls_s.nextval
INTO l_template_dtl_id
    FROM  dual;
  
  BEGIN
  INSERT INTO jai_item_templ_dtls(template_dtl_id
                                ,template_hdr_id
                                ,attribute_code
                                ,attribute_value
                                ,regime_id
                                ,copied_from_template_id
                                ,user_overridden_flag
                                ,applicable_flag
                                ,creation_date
                                ,created_by
                                ,last_update_date
                                ,last_update_login
                                ,last_updated_by
                                ,record_type_code)
                            VALUES(l_template_dtl_id
                                  ,l_template_hdr_id_value
                                  ,k.attribute_code
                                  ,decode(k.attribute_code , 'RECOVERABLE' ,'Y' ,'')   -- Y
                                  ,10000
                                  ,''  --10080 -- 308308 or null
                                  ,''
                                  ,'Y'
                                  ,SYSDATE
                                  ,5562  --FND_GLOBAL.USER_ID
                                  ,SYSDATE
                                  ,-1 -- FND_GLOBAL.LOGIN_ID
                                  ,5562 -- FND_GLOBAL.USER_ID
                                  ,'DEFINED');
     EXCEPTION 
     WHEN OTHERS THEN
     DBMS_OUTPUT.PUT_LINE('Error while inserting into table jai_item_templ_dtls -- '|| sqlerrm);
     END ;                      
 
 END LOOP ;  
EXCEPTION 
WHEN OTHERS THEN
NULL ; 
END ;  

END IF ;  
  END IF ;  
COMMIT ;

BEGIN
SELECT template_hdr_id
INTO l_jai_reporting_value 
FROM  jai_item_templ_hdr
WHERE inventory_item_id = i.inventory_item_id
 AND organization_id =  i.organization_id
 AND entity_type_code IN (  'ITEM_TEMPL_ASGN' ,  'ITEM')
      AND item_classification = 'PRFG' ;
EXCEPTION
WHEN OTHERS THEN 
l_jai_reporting_value := NULL ; 
END ;

IF l_jai_reporting_value IS NOT NULL THEN   
SELECT count(1)
INTO l_count_rep
FROM  jai_reporting_associations
WHERE entity_id = l_jai_reporting_value  ;
 
IF l_count_rep =0 THEN  
  SELECT jai_reporting_associations_s.nextval
    INTO l_report_association_id
      FROM   dual;
      
   SELECT  jai_reporting_codes_s.nextval  
        INTO l_report_code_id
      FROM   dual;
 
DBMS_OUTPUT.put_line('before inserting in jai_reporting_associations');  
  
  BEGIN
  INSERT INTO jai_reporting_associations
      (reporting_association_id
      ,reporting_type_name
      ,reporting_usage
      ,reporting_code_description
      ,reporting_code
      ,entity_code
      ,entity_id
      ,entity_source_table
      ,effective_from
      ,effective_to
      ,creation_date
      ,created_by
      ,last_update_date
      ,last_update_login
      ,last_updated_by
      ,record_type_code
      ,reporting_code_id
      ,reporting_type_id
      ,regime_id
      ,stl_hdr_id) 
    VALUES
      (l_report_association_id
      ,'HSN Code Type for GST'
      ,'LR'
      ,I.hsn_code
      ,I.hsn_code
      ,'ITEM'
      ,l_jai_reporting_value
      ,'JAI_ITEM_TEMPL_HDR'
      ,'02-JAN-2017'
      ,''
      ,SYSDATE
      ,5562 --FND_GLOBAL.USER_ID
      ,SYSDATE
      ,-1 -- FND_GLOBAL.LOGIN_ID 
      ,5562 --FND_GLOBAL.USER_ID
      ,'DEFINED'
      ,l_report_code_id
      ,10000 
      ,''  --10000
      ,''
       );
     EXCEPTION 
     WHEN OTHERS THEN
     DBMS_OUTPUT.PUT_LINE('Error while inserting into table jai_reporting_associations -- '|| sqlerrm);
     END ;

END IF ;
 END IF ; 
 END LOOP ;
 END ;

GST Scripts in Oracle Apps for direct Update

Script to update GST  information in Oracle Apps India Localization screens .


--> First create a dummy table (xxx_vendor_gst_codes ) and insert all supplier and GST information in this table.
--> Below script is used to insert the data directly in setup table jai_party_regs ,jai_party_reg_lines 

DECLARE

l_count NUMBER := NULL ; 
l_vendor_id NUMBER := NULL ; 
l_vendor_site_id NUMBER := NULL ; 
l_party_reg_id NUMBER := NULL ; 
L_ORG_ID NUMBER := NULL ; 
l_party_reg_id_value NUMBER := NULL ; 

CURSOR c1 is 
SELECT distinct vendor_id , vendor_site_id , org_id  , GST_NUMBER FROM xxx.xxx_vendor_gst_codes;


BEGIN

FOR I in c1 loop 

  
  SELECT count(1)
    INTO l_count
 FROM jai_party_regs
  WHERE party_id = i.vendor_id
    AND party_site_id = i.vendor_site_id
AND org_id = i.ORG_ID ;

IF l_count = 0 THEN  

BEGIN


 SELECT  jai_party_regs_s.NEXTVAL
  INTO l_party_reg_id
  FROM DUAL ;



INSERT INTO jai_party_regs 
                  (party_reg_id, 
   party_type_code,
SUPPLIER_FLAG ,
CUSTOMER_FLAG,
                   party_id,
                   party_site_id,
                   org_id,                                
                   org_classification_code, 
  creation_date,
                   created_by, 
  last_update_date, 
  last_update_login,
                   last_updated_by,
                   record_type_code
                  )
           VALUES (l_party_reg_id,               
          'THIRD_PARTY_SITE',
  'Y',
  'N',
   i.vendor_id ,
i.vendor_site_id,
                    i.org_id,
                   '',
  SYSDATE,
                   5562, 
  SYSDATE,
  -1,
                   5562,
                   'USER DEFINED'   );
     EXCEPTION 
WHEN OTHERS THEN 
DBMS_OUTPUT.put_line('Error while inserting in jai_party_regs');
END ;
  
  END IF ;                 
  
  COMMIT ;

  
  BEGIN
   SELECT party_reg_id
    INTO l_party_reg_id_value 
 FROM jai_party_regs
  WHERE party_id = i.vendor_id
    AND party_site_id = i.vendor_site_id
AND org_id = i.ORG_ID ;
EXCEPTION 
WHEN OTHERS THEN
NULL ;
END ;
IF l_party_reg_id_value IS NOT NULL THEN 
  NULL ; 

  BEGIN
INSERT INTO jai_party_reg_lines 
                  (party_reg_id, 
   party_reg_line_id,
                   line_context, 
  regime_id,
                   registration_type_code,
                   registration_number,
                   effective_from, 
  effective_to,
                   creation_date, 
  created_by, 
  last_update_date,
                   last_update_login, 
  last_updated_by,
                   record_type_code
                  )
           VALUES (l_party_reg_id_value,               
          jai_party_reg_lines_s.NEXTVAL ,
  'REGISTRATIONS',
  10000,
   'GST' ,
i.GST_NUMBER,
                   '02-JAN-2017',
  '',
  SYSDATE,
                   5562, 
  SYSDATE,
  -1,
                   5562,
                   'USER DEFINED'   );
     EXCEPTION 
WHEN OTHERS THEN 
DBMS_OUTPUT.put_line('Error while inserting in jai_party_reg_lines');
END ;
  END IF ;  
END LOOP ;
END ;

OAF Mandatory Profiles to enable the Personalization in Oracle Apps .


Profile Name
Value
FND: Personalization Region Link Enabled
Yes
Personalize Self-Service Defn
Yes
Disable Self-Service Personal
No