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'