Create Procedure with no Parameters - oracle

Its my second time writing procedures and this one is the first time I have to use a cursor. For this procedure I am not expected to use parameters but Im having problems with it. This is what I currently have:
Create or Replace Procedure Update_Balance as
Cursor C1 is
Select purchases.Cust_ID, Purchase_Amount, curr_balance,
credit_line,Pending_Flag
from Purchases
inner join customers on purchases.cust_id = customers.cust_id
where purchases.pending_flag = 1
for update of curr_balance;
PendingBalance purchases.purchase_amount%type;
PurchaseRow c1%RowType;
ProposedNewBalance purchases.purchase_amount%type;
Begin
Begin
Open C1;
Fetch c1 into PurchaseRow;
While c1% Found Loop
Select sum(Purchase_amount)
into PendingBalance
from purchases
where cust_id = c1.cust_id
and pending_flag = 1;
end;
ProposedNewBalance := PendingBalance + c1.curr_balance;
If ProposedNewBalance > C1.Credit_Line then
dbms_output.put_line('One or more purchases were not processed for Customer');
end if;
If ProposedNewBalance <= c1.Credit_Line then
update Customers
set curr_balance = ProposedNewBalance
where customer.cust_id = c1.cust_id;
end if;
If ProposedNewBalance <= c1.credit_line then
Update Purchases
set Pending_Flag = 1
where purchases.cust_id = c1.cust_id;
end if;
end;

It would be better if you posted CREATE TABLE statements within the question, not as comments. Besides, you didn't post all tables involved (those referenced by referential integrity constraints so I removed them):
SQL> CREATE TABLE customers(
2 cust_id CHAR(6)
3 CONSTRAINT customers_pk PRIMARY KEY,
4 first_name VARCHAR2(100),
5 last_name VARCHAR2(100),
6 credit_line NUMBER(10,2),
7 curr_balance NUMBER(10,2),
8 earned_points NUMBER(5),
9 tier_id CHAR(2)
10 -- CONSTRAINT tier_fk FOREIGN KEY(tier_id)
11 -- REFERENCES rewards_tier,
12 -- CONSTRAINT balance_chk CHECK(curr_balance <= credit_line)
13 );
Table created.
SQL>
SQL> CREATE TABLE purchases(
2 purchase_id CHAR(7)
3 CONSTRAINT purchase_pk PRIMARY KEY,
4 cust_id CHAR(6),
5 purchase_date DATE,
6 purchase_amount NUMBER(10,2),
7 pending_flag NUMBER(1)
8 -- a value of 1 means it is pending
9 -- CONSTRAINT cust_pur_fk FOREIGN KEY(cust_id)
10 -- REFERENCES customers
11 );
Table created.
SQL>
Now, the procedure:
it is missing END LOOP (so I added it)
it contains one BEGIN-END pair that isn't necessary (so I removed it)
cursor name is c1, but you don't reference columns it returns by cursor name - you have to use cursor variable name (purchaserow) instead
This compiles; you said that you don't know whether what you're doing is correct or not. How are we supposed to know it? You never explained what problem you're solving.
SQL> CREATE OR REPLACE PROCEDURE update_balance AS
2 CURSOR c1 IS
3 SELECT purchases.cust_id,
4 purchase_amount,
5 curr_balance,
6 credit_line,
7 pending_flag
8 FROM purchases
9 INNER JOIN customers ON purchases.cust_id = customers.cust_id
10 WHERE purchases.pending_flag = 1
11 FOR UPDATE OF curr_balance;
12
13 pendingbalance purchases.purchase_amount%TYPE;
14 purchaserow c1%rowtype;
15 proposednewbalance purchases.purchase_amount%TYPE;
16 BEGIN
17 OPEN c1;
18 FETCH c1 INTO purchaserow;
19 WHILE c1%found LOOP
20 SELECT SUM(purchase_amount)
21 INTO pendingbalance
22 FROM purchases
23 WHERE cust_id = purchaserow.cust_id -- this
24 AND pending_flag = 1;
25
26 proposednewbalance := pendingbalance + purchaserow.curr_balance;
27 IF proposednewbalance > purchaserow.credit_line THEN
28 dbms_output.put_line('One or more purchases were not processed for Customer');
29 END IF;
30 IF proposednewbalance <= purchaserow.credit_line THEN
31 UPDATE customers
32 SET
33 curr_balance = proposednewbalance
34 WHERE customers.cust_id = purchaserow.cust_id; -- this
35
36 END IF;
37
38 IF proposednewbalance <= purchaserow.credit_line THEN
39 UPDATE purchases
40 SET
41 pending_flag = 1
42 WHERE purchases.cust_id = purchaserow.cust_id;
43
44 END IF;
45 END LOOP; -- this
46 END;
47 /
Procedure created.
SQL>

Related

I want to fetch column values from column name rollnoofstud of tableA using PL/SQL

DECLARE
TYPE norollno IS TABLE OF VARCHAR2(100);
rollno norollno;
BEGIN
BEGIN
SELECT token
BULK COLLECT INTO rollno
FROM tableA
WHERE columname='rollnoofstud';
EXCEPTION
WHEN NO_DATA_FOUND THEN
rollno := norollno();
END ;
IF rollno >0 THEN
FOR i IN rollno.FIRST..norollno.LAST
LOOP
<doSomeThing>
END LOOP;
END IF;
END;
I am trying this but I am not getting output. I doubt if my select statement is correct.
I don't have your table so I created one:
SQL> CREATE TABLE tablea
2 AS
3 SELECT ename AS token, 'rollnoofstud' AS columname
4 FROM emp
5 WHERE deptno = 10;
Table created.
Code you posted isn't that wrong; requires a little bit of fixing (see line #17, the way you check whether collection contains something (count it!); typo in FOR loop):
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
2 TYPE norollno IS TABLE OF VARCHAR2 (100);
3
4 rollno norollno;
5 BEGIN
6 BEGIN
7 SELECT token
8 BULK COLLECT INTO rollno
9 FROM tableA
10 WHERE columname = 'rollnoofstud';
11 EXCEPTION
12 WHEN NO_DATA_FOUND
13 THEN
14 rollno := norollno ();
15 END;
16
17 IF rollno.COUNT > 0
18 THEN
19 FOR i IN rollno.FIRST .. rollno.LAST
20 LOOP
21 DBMS_OUTPUT.put_line (rollno (i));
22 END LOOP;
23 END IF;
24 END;
25 /
CLARK --> here's the result
KING
MILLER
PL/SQL procedure successfully completed.
SQL>
[EDIT: with your sample table and data:]
(note that there's no text datatype in Oracle!)
SQL> CREATE TABLE students
2 (
3 rollnostud INTEGER PRIMARY KEY,
4 name VARCHAR2 (10) NOT NULL,
5 gender VARCHAR2 (1) NOT NULL
6 );
Table created.
SQL> INSERT INTO students
2 VALUES (1, 'Ryan', 'M');
1 row created.
SQL> INSERT INTO students
2 VALUES (2, 'Joanna', 'F');
1 row created.
SQL> INSERT INTO students
2 VALUES (3, 'John', 'M');
1 row created.
Procedure:
SQL> SET SERVEROUTPUT ON
SQL>
SQL> DECLARE
2 TYPE norollno IS TABLE OF VARCHAR2 (100);
3
4 rollno norollno;
5 BEGIN
6 BEGIN
7 SELECT name
8 BULK COLLECT INTO rollno
9 FROM students;
10 EXCEPTION
11 WHEN NO_DATA_FOUND
12 THEN
13 rollno := norollno ();
14 END;
15
16 IF rollno.COUNT > 0
17 THEN
18 FOR i IN rollno.FIRST .. rollno.LAST
19 LOOP
20 DBMS_OUTPUT.put_line (rollno (i));
21 END LOOP;
22 END IF;
23 END;
24 /
Ryan
Joanna
John
PL/SQL procedure successfully completed.
SQL>

Oracle renaming auto generated list paritions

I have several environments where automatic list partitioning is implemented.
Some product managers don't like the system GENERATED partition name, which is being GENERATED and want to reference the partition by a more meaningful name.
In an effort to address those concerns I'm trying to create a procedure, which will rename the partition based on the high_value. In the example below I am partitioning by state so the names I want to generate are P_OHIO, P_IOWA..
In the procedure below I am getting the following error:
ORA-14048: a partition maintenance operation may not be combined with other operations ORA-06512:
Can someone please advise me on why I am getting this error and how to rectify it.
Thanks in advance for your help and expertise.
CREATE TABLE T21
(
seq_num NUMBER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
num NUMBER(*,0),
state VARCHAR2(20)
)
SEGMENT CREATION DEFERRED
PARTITION BY LIST (state) AUTOMATIC
(PARTITION P_CALIFORNIA VALUES ('CALIFORNIA')
);
insert into t21 (num, state)
select
level * round(dbms_random.value(1,50)),
case round(dbms_random.value(1,50))
when 1 then 'Alabama'
when 2 then 'Alaska'
when 3 then 'Arizona'
when 4 then 'Arkansas'
when 5 then 'California'
when 6 then 'Colorado'
when 7 then 'Connecticut'
when 8 then 'Delaware'
when 9 then 'Florida'
when 10 then 'Georgia'
when 11 then 'Hawaii'
when 12 then 'Idaho'
when 13 then 'Illinois'
when 14 then 'Indiana'
when 15 then 'Iowa'
when 16 then 'Kansas'
when 17 then 'Kentucky'
when 18 then 'Louisiana'
when 19 then 'Maine'
when 20 then 'Maryland'
when 21 then 'Massachusetts'
when 22 then 'Michigan'
when 23 then 'Minnesota'
when 24 then 'Mississippi'
when 25 then 'Missouri'
when 26 then 'Montana'
when 27 then 'Nebraska'
when 28 then 'Nevada'
when 29 then 'New Hampshire'
when 30 then 'New Jersey'
when 31 then 'New Mexico'
when 32 then 'New York'
when 33 then 'North Carolina'
when 34 then 'North Dakota'
when 35 then 'Ohio'
when 36 then 'Oklahoma'
when 37 then 'Oregon'
when 38 then 'Pennsylvania'
when 39 then 'Rhode Island'
when 40 then 'South Carolina'
when 41 then 'South Dakota'
when 42 then 'Tennessee'
when 43 then 'Texas'
when 44 then 'Utah'
when 45 then 'Vermont'
when 46 then 'Virginia'
when 47 then 'Washington'
when 48 then 'West Virginia'
when 49 then 'Wisconsin'
when 50 then 'Wyoming'
end
from dual
connect by level <= 100;
SELECT TABLE_NAME, PARTITION_NAME, HIGH_VALUE FROM USER_TAB_PARTITIONS WHERE TABLE_NAME ='T21';
CREATE OR REPLACE PROCEDURE RenameListPartitions
IS
CURSOR PartTables IS
SELECT TABLE_NAME
FROM USER_PART_TABLES
WHERE PARTITIONING_TYPE = 'LIST'
ORDER BY TABLE_NAME;
CURSOR TabParts(aTableName VARCHAR2) IS
SELECT PARTITION_NAME, HIGH_VALUE
FROM USER_TAB_PARTITIONS
WHERE regexp_like(partition_name,'^SYS_P[[:digit:]]{1,10}') AND
TABLE_NAME = aTableName AND
table_name not like 'BIN$%'
ORDER BY PARTITION_POSITION;
newPartName VARCHAR2(30);
BEGIN
FOR aTab IN PartTables LOOP
FOR aPart IN TabParts(aTab.TABLE_NAME) LOOP
execute immediate 'select ' || aPart.HIGH_VALUE || ' from dual' into newPartName;
IF newPartName <> aPart.PARTITION_NAME THEN
EXECUTE IMMEDIATE 'ALTER TABLE '||aTab.TABLE_NAME||' RENAME PARTITION '||aPart.PARTITION_NAME||' TO '||newPartName;
END IF;
END LOOP;
END LOOP;
END RenameListPartitions;
/
EXEC RenameListPartitions;
Some of your partition values have spaces - like 'New York' - so you need to quote the identifiers for the partitions:
EXECUTE IMMEDIATE 'ALTER TABLE '||aTab.TABLE_NAME
||' RENAME PARTITION '||aPart.PARTITION_NAME
||' TO "'||newPartName||'"';
db<>fiddle
Of course, that means you also need to quote them when you refer to them explicitly in the future; it might be better to, say, replace spaces with underscores and change to uppercase, so you get NEW_YORK:
EXECUTE IMMEDIATE 'ALTER TABLE '||aTab.TABLE_NAME
||' RENAME PARTITION '||aPart.PARTITION_NAME
||' TO '||upper(replace(newPartName,' ','_'));
db<>fiddle

plsql what is wrong with this code error 'INTO list is of wrong type'

Can someone please help me? Why do I get this error "INTO list is of wrong type"?
DECLARE
TYPE v_dept_table IS TABLE OF DEPARTMENTS.DEPARTMENT_NAME%TYPE INDEX BY PLS_INTEGER;
v_record_dept_table v_dept_table;
v_loop_count NUMBER := 10;
v_dept_no NUMBER := 1;
BEGIN
FOR v_dept_no IN 1..v_loop_count LOOP
SELECT DEPARTMENTS.DEPARTMENT_NAME
INTO v_record_dept_table
FROM DEPARTMENTS
WHERE DEPARTMENT_ID = v_dept_no;
v_dept_no := v_dept_no + 1;
INSERT
INTO v_dept_table
VALUES v_record_dept_table;
END LOOP;
FOR indx IN NVL (v_dept_table.FIRST, 0) .. NVL (v_dept_table.LAST, -1)
LOOP
DBMS_OUTPUT.PUT_LINE(v_dept_table(indx));
END LOOP;
END;
ORA-06550: line 16, column 14:
PLS-00597: expression 'V_RECORD_DEPT_TABLE' in the INTO list is of wrong type
Why is it a wrong type? I'm using the hr schema from Oracle
Not exactly like that; you've done several things wrong. I tried to fix your code (with as little modifications as possible); have a look, read comments I wrote, compare it to your code. Side note: I used Scott's DEPT table as I don't have your DEPARTMENTS.
SQL> set serveroutput on
SQL> DECLARE
2 TYPE v_dept_table IS TABLE OF dept.dname%TYPE
3 INDEX BY PLS_INTEGER;
4
5 v_record_dept_table v_dept_table;
6
7 v_loop_count NUMBER := 10;
8 v_dept_no NUMBER := 1;
9 BEGIN
10 FOR v_dept_no IN 1 .. v_loop_count
11 LOOP
12 BEGIN
13 SELECT dname
14 INTO v_record_dept_table (v_dept_no) --> you're missing "(v_dept_no)"
15 FROM dept
16 WHERE deptno = v_dept_no;
17 -- Don't manually increment FOR loop variable; Oracle does it itself
18 -- v_dept_no := v_dept_no + 1;
19
20 -- You can't insert into "type"; besides, you've already inserted into V_RECORD_DEPT_TABLE.
21 -- INSERT INTO v_dept_table VALUES v_record_dept_table;
22 EXCEPTION
23 WHEN NO_DATA_FOUND
24 THEN
25 NULL;
26 END;
27 END LOOP;
28
29 -- loop through V_RECORD_DEPT_TABLE (collection), not V_DEPT_TABLE (type). No need for NVL.
30 FOR indx IN NVL (v_record_dept_table.FIRST, 0) ..
31 NVL (v_record_dept_table.LAST, -1)
32 LOOP
33 DBMS_OUTPUT.PUT_LINE (v_record_dept_table (indx));
34 END LOOP;
35 END;
36 /
ACCOUNTING
PL/SQL procedure successfully completed.
SQL>
Alternatively, see whether this helps. I used built-in type (sys.odcivarchar2list) and BULK COLLECT INTO (performs better).
SQL> DECLARE
2 v_record_dept_table SYS.odcivarchar2list;
3 BEGIN
4 SELECT dname
5 BULK COLLECT INTO v_record_dept_table
6 FROM dept;
7
8 FOR indx IN v_record_dept_table.FIRST .. v_record_dept_table.LAST
9 LOOP
10 DBMS_OUTPUT.PUT_LINE (v_record_dept_table (indx));
11 END LOOP;
12 END;
13 /
ACCOUNTING
RESEARCH
SALES
OPERATIONS
PL/SQL procedure successfully completed.
SQL>

oracle delete data from table with rownum

I have a procedure that deletes the data from table1. Five instances of the procedure running the same time and all ran without any error/exception.
But still, there are many records in the table1 where EndTime <= v_purgedate. Please help.
declare
rowcount1 NUMBER;
begin
LOOP
delete table1 eh
where eh.EndTime <= v_purgedate
and rownum <= 30000 ;
rowcount1 := rowcount1 + sql%rowcount;
commit;
exit when rowcount1=0;
rowcount1=0;
END LOOP;
end;
/
You are trying to do what is called "do it yourself parallelism", as opposed to the parallel processing that Oracle can do by itself if you ask nicely.
There is a package called DBMS_PARALLEL_EXECUTE that does this for you. You split the data into chunks (based on ROWIDs or numbers), then call the RUN_TASK procedure to process each chunk, either serially or in parallel. There is one commit per chunk.
Here is a demonstration that deletes 30,000 rows per commit. First, test data:
SQL> create table table1(endtime) as
2 select trunc(sysdate) - level/24 from dual
3 connect by level <= 240000;
Table TABLE1 created.
SQL> insert into table1 select * from table1;
240,000 rows inserted.
SQL> insert into table1 select * from table1;
480,000 rows inserted.
SQL> insert into table1 select * from table1;
960,000 rows inserted.
SQL> select count(*) from table1;
COUNT(*)
----------
1920000
Now, here is the code:
SQL> declare
2 l_task_name varchar2(128) := 'Delete_TABLE1';
3 l_sql_chunk clob := q'§
4 select date '2100-01-01' - min(endtime) start_id,
5 date '2100-01-01' - max(endtime) end_id,
6 chunk
7 from (
8 select endtime,
9 ceil(row_number() over(order by endtime) / 30000) chunk
10 from table1 where endtime < sysdate - 8000
11 )
12 group by chunk
13 §';
14 l_sql_run clob := q'§
15 delete from table1
16 where endtime between
17 date '2100-01-01' - :start_id and
18 date '2100-01-01' - :end_id
19 §';
20 l_boolean boolean := false;
21 task_not_found EXCEPTION;
22 PRAGMA EXCEPTION_INIT(task_not_found, -29498);
23 begin
24 begin
25 DBMS_PARALLEL_EXECUTE.DROP_TASK (l_task_name);
26 exception when task_not_found then null;
27 end;
28 DBMS_PARALLEL_EXECUTE.CREATE_TASK (l_task_name);
29 DBMS_PARALLEL_EXECUTE.CREATE_CHUNKS_BY_SQL (
30 task_name => l_task_name,
31 sql_stmt => l_sql_chunk,
32 by_rowid => l_boolean
33 );
34 DBMS_PARALLEL_EXECUTE.RUN_TASK (
35 task_name => l_task_name,
36 sql_stmt => l_sql_run,
37 language_flag => 1,
38 parallel_level => 0 -- 0 for serial, 2+ for parallel
39 );
40 end;
41 /
you have an endless Loop, because rowcount1 will never be 0 when your table contains data that will be deleted.
i think what you what is
declare
rowcount1 NUMBER;
begin
LOOP
delete table1 eh
where eh.EndTime <= v_purgedate -- you should also initialize a variable
and rownum <= 30000 ;
--rowcount1 := rowcount1 + sql%rowcount;
commit;
exit when sql%rowcount = 0;
rowcount1=0;
END LOOP;
end;
/
When you declare the variable rowcount1, you do not assign any number to it, so its value is NULL. Afterwards, when you add a number to NULL the result is always NULL. This is surely not what you want. To illustrate:
SQL> declare
2 rowcount1 NUMBER;
3 begin
4 dbms_output.put_line('<'||rowcount1||'>');
5 rowcount1 := rowcount1 + 1;
6 dbms_output.put_line('<'||rowcount1||'>');
7 end;
8 /
<>
<>
PL/SQL procedure successfully completed.
It seems your code will go in an endless loop. You say that is not the case, so clearly you are not showing us any real code.
Anyway, this is not a good way to do what you want. I'll post another answer saying why.
Regards, Stew Ashton
It's a easy code for delete data in a table .
Try it.
declare
cursor c is
select * from table1 where rownum<=30000;
begin
for i in c loop
delete from table1 where EndTime <= v_purgedate;
end loop;
commit;
end;

Wrong calculation for daily partitions

Oracle : 11.2.0.2
I'm trying to drop monthy and daily partitions using a script. This works fine for monthly partitions but not for daily partitions. Below is the error I see in the log. Day of the month is becoming zero when calculating.
2013-08-0|SYS_P328538|2|YES
DECLARE
*
ERROR at line 1:
ORA-01847: day of month must be between 1 and last day of month
ORA-06512: at line 43
Here below is the script. I think highvalue date is miscalculated.
SQL> DECLARE
2 CURSOR tab_part_cur IS
3 select PARTITION_POSITION, PARTITION_NAME,HIGH_VALUE,INTERVAL from dba_tab_partitions where table_name = 'MO_USAGEDATA'
and table_owner = 'WSMUSER17'
order by PARTITION_POSITION;
4 tab_part_rec tab_part_cur%ROWTYPE;
5 lHighValue LONG;
6 strPartitionLessThanDate VARCHAR2(100);
7 dtTestDate DATE;
8 DaysInPast NUMBER;
9 SQLstr varchar2(100);
10 strIntervalType varchar2(1000);
11 strRunType varchar2(20);
12 BEGIN
13 strRunType := 'DRY_RUN';
14 select INTERVAL into strIntervalType from dba_part_tables where table_name ='MO_USAGEDATA' and owner = 'WSMUSER17';
15 strIntervalType := REGEXP_SUBSTR(strIntervalType, '''[^'']+''');
16 DBMS_OUTPUT.PUT_LINE(strIntervalType);
17 CASE
18 WHEN strIntervalType = '''DAY''' THEN
19 DBMS_OUTPUT.PUT_LINE('Interval type = '||strIntervalType);
20 -- dtTestDate := CURRENT_DATE - 7 - 1; Offset adjustment if necessary
21 dtTestDate := CURRENT_DATE - 7;
22 DBMS_OUTPUT.PUT_LINE('Test Date = '||dtTestDate);
23 WHEN strIntervalType = '''MONTH''' THEN
24 DBMS_OUTPUT.PUT_LINE('Interval type = '||strIntervalType);
25 -- dtTestDate := CURRENT_DATE - 90;
26 dtTestDate := ADD_MONTHS(current_date,- 7);
27 DBMS_OUTPUT.PUT_LINE('TestDate = '||dtTestDate);
28 ELSE
29 DBMS_OUTPUT.PUT_LINE('Unexpected interval, exiting.');
30 GOTO EXIT;
31 END CASE;
32 OPEN tab_part_cur;
33 LOOP
34 FETCH tab_part_cur INTO tab_part_rec;
35 EXIT WHEN tab_part_cur%NOTFOUND;
36 DBMS_OUTPUT.PUT_LINE(tab_part_cur%ROWCOUNT);
37 lHighValue := tab_part_rec.high_value;
38 /* This next line seems redundant but is needed for conversion quirk from LONG to VARCHAR2
39 */
40 strPartitionLessThanDate := lHighValue;
41 strPartitionLessThanDate := substr(strPartitionLessThanDate, 11, 10);
42 DBMS_OUTPUT.PUT_LINE(strPartitionLessThanDate ||'|'|| tab_part_rec.partition_name ||'|'|| tab_part_rec.partition_position ||'|'|| tab_part_rec.interval);
43 DBMS_OUTPUT.PUT_LINE(TO_DATE(strPartitionLessThanDate, 'YYYY-MM-DD') ||'******'||dtTestDate);
44 IF TO_DATE(strPartitionLessThanDate, 'YYYY-MM-DD') < dtTestDate AND tab_part_rec.partition_name <> 'PART_MINVALUE
' THEN
45 SQLstr := 'ALTER TABLE WSMUSER17.MO_USAGEDATA DROP PARTITION '||tab_part_rec.partition_name ||' update Global indexes';
46 DBMS_OUTPUT.PUT_LINE('Targeted Partition !!!!!!!!');
47 IF strRunType = 'LIVE_RUN' THEN
48 DBMS_OUTPUT.PUT_LINE('Dropping Partition !!!!!!!!');
49 execute immediate SQLstr;
50 END IF;
51 END IF;
52 END LOOP;
53 CLOSE tab_part_cur;
54 << EXIT >>
55 DBMS_OUTPUT.PUT_LINE('Partition purge complete');
56 END;
57 /
'DAY'
Interval type = 'DAY'
Test Date = 03-SEP-13
1
2012-06-1|PART_MINVALUE|1|NO
01-JUN-12******03-SEP-13
2
2013-08-0|SYS_P328538|2|YES
DECLARE
*
ERROR at line 1:
ORA-01847: day of month must be between 1 and last day of month
ORA-06512: at line 43
I'm trying to keep lat 7 partitions in the daily partitioned table and drop the rest of the partitions. But its not dropping them.
Ok, I created the table, inserted some data and ran some of your queries and you've got something wrong with your substring:
SQL> CREATE TABLE "MO_USAGEDATA" (
2 "REQUESTDTS" TIMESTAMP (9) NOT NULL ENABLE
3 )
4 partition by range ("REQUESTDTS") INTERVAL(NUMTODSINTERVAL(1,'DAY'))
5 (partition PART_MINVALUE values less than(TIMESTAMP '2012-06-18 00:00:00'));
Table created
SQL> INSERT INTO MO_USAGEDATA
2 (SELECT SYSDATE + ROWNUM FROM dual CONNECT BY LEVEL <= 30);
30 rows inserted
SQL> SELECT high_value, INTERVAL
2 FROM all_tab_partitions
3 WHERE table_name = 'MO_USAGEDATA'
4 AND table_owner = USER
5 ORDER BY PARTITION_POSITION;
HIGH_VALUE INTERVAL
------------------------------------ ---------
[...]
TIMESTAMP' 2013-09-30 00:00:00' YES
TIMESTAMP' 2013-10-01 00:00:00' YES
TIMESTAMP' 2013-10-02 00:00:00' YES
[...]
SQL> SELECT substr('TIMESTAMP'' 2013-10-02 00:00:00''', 11, 10) FROM dual;
SUBSTR('TIMESTAMP''2013-10-020
------------------------------
2013-10-0
As you can see you're off by one character. It works with DATE columns, but for TIMESTAMP partitionning, you'll need to adjust the offset.

Resources