Multiple Output values in a dynamic sql [message #608549] |
Thu, 20 February 2014 22:09 |
|
sandba00
Messages: 10 Registered: February 2014
|
Junior Member |
|
|
Hi,
I am trying to write a query to dynamically loop through the partitions in the table and fetch the date and count of records for each date.
Since I have a million of records for each day partition, I decided to use dynamic sql as below. When I try to use only count, I am able to get the output,. but since I am trying to get multiple values from dynamic sql, its not giving an output. Please suggest me some ideas to get multiple values in the output.
Accept prompt 'Number of days of report:'
DECLARE
i NUMBER;
dt VARCHAR2(1000);
cnt VARCHAR2(1000);
x VARCHAR2(1000);
start_day NUMBER;
end_day NUMBER;
BEGIN
i := &1;
SELECT To_char(Trunc(SYSDATE - i), 'RRRRMMDD')
INTO start_day
FROM dual;
SELECT To_char(Trunc(SYSDATE), 'RRRRMMDD')
INTO end_day
FROM dual;
IF ( i < 10 ) THEN
FOR a IN (SELECT partition_name
FROM user_tab_partitions
WHERE table_name = 'PART_TEST_DETAILS'
AND Substr(partition_name, 10) >= start_day
AND Substr(partition_name, 10) < end_day) LOOP
BEGIN
x :=
'select TRUNC(process_date),COUNT(*) from PART_TEST_DETAILS partition('
||a.partition_name
||') where status in (''J'',''K'',''S'') group by trunc(process_date)'
;
dbms_output.Put_line(x);
EXECUTE IMMEDIATE x INTO dt,
cnt;
dbms_output.Put_line(dt
||' '
||cnt);
EXCEPTION
WHEN OTHERS THEN
NULL;
dbms_output.Put_line('No Records for this partition');
END;
END LOOP;
ELSE
dbms_output.Put_line ('Number of days is greater than 10');
END IF;
END;
/
*BlackSwan formatted {code} using http://www.dpriver.com/pp/sqlformat.htm?ref=g_wangz
Please do so yourself in the future.
[Updated on: Thu, 20 February 2014 22:14] by Moderator Report message to a moderator
|
|
|
|
|
|
Re: Multiple Output values in a dynamic sql [message #608565 is a reply to message #608556] |
Fri, 21 February 2014 02:18 |
|
sandba00
Messages: 10 Registered: February 2014
|
Junior Member |
|
|
Michel,
I did not get you..
But If I my understanding is right, the query you gave works perfectly.
Since I have daily partitions and table size is huge in production, I am trying to loop through each days partitions.
When I have multiple entries, then it is failing.
Quote:
Example. I may have 2 dates of records in process_date column (for 18-FEB-2014 and 19-FEB-2014) for PART_TEST_19022014 partition.
Process_date Count(1)
-------------- ---------------
18-FEB-2014 13443
18-FEB-2014 9433
19-FEB-2014 34349
If I have single date , its successful
Quote:
Example for partition PART_TEST_19022014 , if I have process date of only either 18-FEB-2014 or 19-FEB-2014.
Process_date Count(1)
-------------- ---------------
18-FEB-2014 13443
This is because, the partition is created on another date column.
[Updated on: Fri, 21 February 2014 02:18] Report message to a moderator
|
|
|
Re: Multiple Output values in a dynamic sql [message #608567 is a reply to message #608565] |
Fri, 21 February 2014 02:30 |
|
sandba00
Messages: 10 Registered: February 2014
|
Junior Member |
|
|
Output of both SQL and Pl/Sql
SQL> select TRUNC(process_date),COUNT(*) from PART_TEST_DETAILS partition(PART_TEST_20140220) where status in ('J','K','S') group by trunc(process_date);
TRUNC(PRO COUNT(*)
--------- ----------
20-FEB-14 4772220
21-FEB-14 21097
Result after executing through Pl/sql
Enter value for 1: 1
old 9: i :=&1;
new 9: i :=1;
select TRUNC(process_date),COUNT(*) from PART_TEST_DETAILS partition(PART_TEST_20140220) where status in ('J','K','S') group by trunc(process_date);
No Records for this partition
[Updated on: Fri, 21 February 2014 02:31] Report message to a moderator
|
|
|
|
Re: Multiple Output values in a dynamic sql [message #608580 is a reply to message #608565] |
Fri, 21 February 2014 03:11 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Quote:But If I my understanding is right, the query you gave works perfectly.
Since I have daily partitions and table size is huge in production, I am trying to loop through each days partitions.
If you have a daily partitioned table (which I assumed given the result you wanted), the query I gave will return the same output than looping over the partitions but it will be far faster.
|
|
|