I have a table with partition based on date field. Now, I have to write a procedure to drop all partitions older than 12 months.
I have the entries in the user_tab_partitions table in the following format.
PARTITION_NAME HIGH_VALUE
-------------- ----------
SYS_P28468650 20161221
SYS_P28468649 20161220
SYS_P28468648 20161219
SYS_P28468647 20161218
Kindly help me on how to do it
Looks like partitions are based on number, not DATE values.
Anyway, procedure like this should work:
DECLARE
HighValue NUMBER;
CURSOR TabPartitions IS
SELECT TABLE_NAME, PARTITION_NAME, HIGH_VALUE
FROM USER_TAB_PARTITIONS
WHERE TABLE_NAME = 'YOUR_TABLE'
ORDER BY TABLE_NAME, PARTITION_POSITION;
BEGIN
FOR aPart IN TabPartitions LOOP
EXECUTE IMMEDIATE 'BEGIN :ret := '||aPart.HIGH_VALUE||'; END;' USING OUT HighValue;
IF TO_DATE(HighValue, 'YYYYMMDD') < ADD_MONTHS(SYSDATE, -12) THEN
EXECUTE IMMEDIATE 'ALTER TABLE YOUR_TABLE DROP PARTITION '||aPart.PARTITION_NAME||' UPDATE GLOBAL INDEXES';
END IF;
END LOOP;
END;
You can also use
EXECUTE IMMEDIATE 'ALTER TABLE YOUR_TABLE DROP PARTITION FOR ('||aPart.PARTITION_NAME||') UPDATE GLOBAL INDEXES';
Related
I need a procedure that will help me to drop partition older than 3 months (current month + last 3 older months). And then I need to create the new partition in sequence.
for example if current partition is May22, then I need to drop Jan22 and create Jan23 partition. Can someone help me with the procedure?
Here is the process we use to RENAME and drop PARTITIONs that are range INTERVAL. Just add the procedures to your scheduler.
They work great
CREATE OR REPLACE PROCEDURE ddl(p_cmd varchar2)
authid current_user
is
t1 pls_integer;
BEGIN
t1 := dbms_utility.get_time;
dbms_output.put_line(p_cmd);
execute immediate p_cmd;
dbms_output.put_line((dbms_utility.get_time - t1)/100 || ' seconds');
END;
/
CREATE TABLE PARTITION_RETENTION (
seq_num NUMBER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
TABLE_NAME VARCHAR2(30),
RETENTION INTERVAL DAY(3) TO SECOND(0),
CONSTRAINT
partition_retention_pk primary key (table_name),
CONSTRAINT CHK_NON_ZERO_DAYS CHECK (
RETENTION > INTERVAL '0' DAY
),
CONSTRAINT CHK_WHOLE_DAYS CHECK (
EXTRACT(HOUR FROM RETENTION) = 0
AND EXTRACT(MINUTE FROM RETENTION) = 0
AND EXTRACT(SECOND FROM RETENTION) = 0
)
);
insert into PARTITION_RETENTION (TABLE_NAME, RETENTION)
select 'T1', interval '10' day from dual union all
select 'T3', interval '15' day from dual union all
select 'T4', 15 * interval '1' day from dual union all
select 'T5', 5 * interval '1 00:00:00' day to second from dual;
CREATE TABLE t1 (
seq_num NUMBER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
dt DATE
)
PARTITION BY RANGE (dt)
INTERVAL (NUMTODSINTERVAL(7,'DAY'))
(
PARTITION OLD_DATA values LESS THAN (TO_DATE('2022-01-01','YYYY-MM-DD'))
);
/
INSERT into t1 (dt)
with dt (dt, interv) as (
select date '2022-01-01', numtodsinterval(1,'DAY') from dual
union all
select dt.dt + interv, interv from dt
where dt.dt + interv < date '2022-02-01')
select dt from dt;
/
create index t1_global_ix on t1 (dt);
/
CREATE OR REPLACE PROCEDURE MaintainPartitions IS EXPRESSION_IS_OF_WRONG_TYPE EXCEPTION;
PRAGMA EXCEPTION_INIT(EXPRESSION_IS_OF_WRONG_TYPE, -6550);
CURSOR PartTables IS
SELECT TABLE_NAME, INTERVAL
FROM USER_PART_TABLES
WHERE PARTITIONING_TYPE = 'RANGE'
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$%'
and interval is not null
ORDER BY PARTITION_POSITION;
ym INTERVAL YEAR TO MONTH;
ds INTERVAL DAY TO SECOND;
newPartName VARCHAR2(30);
PERIOD TIMESTAMP;
BEGIN
FOR aTab IN PartTables LOOP
BEGIN
EXECUTE IMMEDIATE 'BEGIN :ret := '||aTab.INTERVAL||'; END;' USING OUT ds;
ym := NULL;
EXCEPTION
WHEN EXPRESSION_IS_OF_WRONG_TYPE THEN
EXECUTE IMMEDIATE 'BEGIN :ret := '||aTab.INTERVAL||'; END;' USING OUT ym;
ds := NULL;
END;
FOR aPart IN TabParts(aTab.TABLE_NAME) LOOP
EXECUTE IMMEDIATE 'BEGIN :ret := '||aPart.HIGH_VALUE||'; END;' USING OUT PERIOD;
IF ds IS NOT NULL THEN
IF ds >= INTERVAL '7' DAY THEN
-- Weekly partition
EXECUTE IMMEDIATE 'BEGIN :ret := TO_CHAR('||aPart.HIGH_VALUE||' - :int, :fmt); END;' USING OUT newPartName, INTERVAL '1' DAY, '"P_"IYYY"W"IW';
ELSE
-- Daily partition
EXECUTE IMMEDIATE 'BEGIN :ret := TO_CHAR('||aPart.HIGH_VALUE||' - :int, :fmt); END;' USING OUT newPartName, INTERVAL '1' DAY, '"P_"YYYYMMDD';
END IF;
ELSE
IF ym = INTERVAL '3' MONTH THEN
-- Quarterly partition
EXECUTE IMMEDIATE 'BEGIN :ret := TO_CHAR('||aPart.HIGH_VALUE||' - :int, :fmt); END;' USING OUT newPartName, INTERVAL '1' DAY, '"P_"YYYY"Q"Q';
ELSE
-- Monthly partition
EXECUTE IMMEDIATE 'BEGIN :ret := TO_CHAR('||aPart.HIGH_VALUE||' - :int, :fmt); END;' USING OUT newPartName, INTERVAL '1' DAY, '"P_"YYYYMM';
END IF;
END IF;
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 MaintainPartitions;
/
CREATE OR REPLACE PROCEDURE rebuild_index
authid current_user
is
BEGIN
for i in (
select index_owner, index_name, partition_name, 'partition' ddl_type
from all_ind_partitions
where status = 'UNUSABLE'
union all
select owner, index_name, null, null
from all_indexes
where status = 'UNUSABLE'
)
loop
if i.ddl_type is null then
ddl('alter index '||i.index_owner||'.'||i.index_name||' rebuild parallel 4 online');
else
ddl('alter index '||i.index_owner||'.'||i.index_name||' modify '||i.ddl_type||' '||i.partition_name||' rebuild parallel 4 online');
end if;
end loop;
END;
/
EXEC MaintainPartitions;
DECLARE
CANNOT_DROP_LAST_PARTITION EXCEPTION;
PRAGMA EXCEPTION_INIT(CANNOT_DROP_LAST_PARTITION, -14758);
CANNOT_DROP_ONLY_ONE_PARTITION EXCEPTION;
PRAGMA EXCEPTION_INIT(CANNOT_DROP_ONLY_ONE_PARTITION, -14083);
ts TIMESTAMP;
CURSOR TablePartitions IS
SELECT TABLE_NAME, PARTITION_NAME, p.HIGH_VALUE, t.INTERVAL, RETENTION, DATA_TYPE
FROM USER_PART_TABLES t
JOIN USER_TAB_PARTITIONS p USING (TABLE_NAME)
JOIN USER_PART_KEY_COLUMNS pk ON pk.NAME = TABLE_NAME
JOIN USER_TAB_COLS tc USING (TABLE_NAME, COLUMN_NAME)
JOIN PARTITION_RETENTION r USING (TABLE_NAME)
WHERE pk.object_type = 'TABLE' AND
t.partitioning_type = 'RANGE' AND
REGEXP_LIKE (tc.data_type, '^DATE$|^TIMESTAMP.*');
BEGIN
FOR aPart IN TablePartitions LOOP
EXECUTE IMMEDIATE 'BEGIN :ret := '||aPart.HIGH_VALUE||'; END;' USING OUT ts;
IF ts < SYSTIMESTAMP - aPart.RETENTION THEN
BEGIN
ddl('alter table '||aPart.TABLE_NAME||' drop partition '||aPart.partition_name);
EXCEPTION
WHEN CANNOT_DROP_ONLY_ONE_PARTITION THEN
DBMS_OUTPUT.PUT_LINE('Cant drop the only partition '||aPart.PARTITION_NAME ||' from table '||aPart.TABLE_NAME);
ddl('ALTER TABLE '||aPart.TABLE_NAME||' TRUNCATE PARTITION '||aPart.PARTITION_NAME);
WHEN CANNOT_DROP_LAST_PARTITION THEN
BEGIN
DBMS_OUTPUT.PUT_LINE('Drop last partition '||aPart.PARTITION_NAME ||' from table '||aPart.TABLE_NAME);
EXECUTE IMMEDIATE 'ALTER TABLE '||aPart.TABLE_NAME||' SET INTERVAL ()';
ddl('alter table '||aPart.TABLE_NAME||' drop partition '||aPart.partition_name);
EXECUTE IMMEDIATE 'ALTER TABLE '||aPart.TABLE_NAME||' SET INTERVAL( '||aPart.INTERVAL||' )';
EXCEPTION
WHEN CANNOT_DROP_ONLY_ONE_PARTITION THEN
-- Depending on the order the "last" partition can be also the "only" partition at the same time
EXECUTE IMMEDIATE 'ALTER TABLE '||aPart.TABLE_NAME||' SET INTERVAL( '||aPart.INTERVAL||' )';
DBMS_OUTPUT.PUT_LINE('Cant drop the only partition '||aPart.PARTITION_NAME ||' from table '||aPart.TABLE_NAME);
ddl('ALTER TABLE '||aPart.TABLE_NAME||' TRUNCATE PARTITION '||aPart.PARTITION_NAME);
END;
END;
END IF;
END LOOP;
rebuild_index();
END;
Use interval partitioning - available starting from Oracle 11.
You will need no procedure for a creation of a partition, because the partition will be created automatically with the first insert of the data with the corresponding date.
You will still need a basic code for drop partition - rolling window is not supported in the interval partitioning.
Using the partition extended names makes it much easier that if you use partition names
Example
Create Interval Partitioned Table
CREATE TABLE test_part
(trans_date DATE,
pad VARCHAR2(100)
)
PARTITION BY RANGE (trans_date)
INTERVAL (NUMTODSINTERVAL(1,'DAY'))
(
PARTITION part_init values LESS THAN (DATE'2020-01-01')
);
Note - use a less than date long in the history for the initial partition so that no data will be inserted there.
This partition will remain empty and will not be dropped, otherwise you encounter
ORA-14083: cannot drop the only partition of a partitioned table.
Create Partition
Now populate that table with few rows.
Note that there is no need for explicite create partition as we use interval partitioning and the partitions are created automatically.
insert into test_part(trans_date, pad)
select add_months(trunc(sysdate),-3)-rownum trans_date, 'X' pad
from dual connect by level <= 3;
Drop Partition
The implenetation of the rolling window consists of two steps
First check the transactions dates that are older than three months
select distinct trunc(trans_date) drop_date from test_part
where trans_date < add_months(trunc(sysdate),-3)
order by 1 desc;
DROP_DATE
-------------------
08.01.2022 00:00:00
07.01.2022 00:00:00
06.01.2022 00:00:00
Now you need to drop partitions for the above selected days.
You can use the *partition extended names to do so, without a need to know the partition name and the ugly LONG HIGH_VALUE.
Example for the day DATE'2022-07-07'
alter table TEST_PART drop partition for (DATE'2022-07-07');
You may pack the clean up logic in a simple procedure
create or replace procedure rolling_window as
v_sql varchar2(4000);
begin
for cur in (
select distinct trunc(trans_date) drop_date from test_part
where trans_date < add_months(trunc(sysdate),-3)
order by 1 desc)
loop
v_sql := q'[alter table TEST_PART drop partition for (DATE']'||
to_char(cur.drop_date,'YYYY-MM-DD')||q'[') ]';
dbms_output.put_line(v_sql);
execute immediate v_sql;
end loop;
end;
/
If you use global indexes you may want to add the clause UPDATE INDEXES in the ALTER TABLEto keep the index usable after the operation.
I'm trying to drop all partitions for a table and I'm running into the following error:
ORA-14083: cannot drop the only partition of a partitioned table ORA-06512:
I know I can drop the table and recreate it but I want to see if my procedure can be modified to use an EXCEPTION that sets the retention to nothing, drops the PARTITION and sets the INTERVAL back to its original value?
Any help and expertise would be greatly appreciated. Below is my test CASE.
CREATE OR REPLACE PROCEDURE ddl(p_cmd varchar2)
authid current_user
is
BEGIN
dbms_output.put_line(p_cmd);
execute immediate p_cmd;
END;
/
CREATE OR REPLACE PROCEDURE
drop_partition(
p_tab varchar2,
p_date date
) authid current_user
is
v_high_value date;
cursor v_cur is
select table_name,
partition_name,
high_value,
partition_position
from user_tab_partitions
where table_name = upper(p_tab);
begin
for v_rec in v_cur loop
execute immediate 'select ' || v_rec.high_value || ' from dual' into v_high_value;
if v_high_value <= trunc(p_date)
then
-- dbms_output.put_line ('partition ' || v_rec.partition_name || ' high value is ' || to_char(v_high_value,'mm/dd/yyyy'));
--ddl( 'ALTER TABLE ' || p_tab || ' DROP PARTITION FOR(DATE ' || TO_CHAR(v_high_value,'YYYY-MM-DD') || ')');
ddl('alter table '||p_tab||' drop partition '||v_rec.partition_name);
end if;
end loop;
END;
/
CREATE TABLE partition_retention
(
seq_num NUMBER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
TABLE_NAME VARCHAR2(30),
DAYS NUMBER(6),
CONSTRAINT
partition_retention_pk primary key (table_name));
/
INSERT into partition_retention(TABLE_NAME, DAYS)
WITH data as (
select 'T1', 0
from dual union all
select 'T3', 15
from dual union all
select 'T4', 10
from dual union all
select 'T5', 5
from dual)
SELECT * from data;
/
CREATE TABLE t1 (
seq_num NUMBER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
dt DATE
)
PARTITION BY RANGE (dt)
INTERVAL (NUMTODSINTERVAL(7,'DAY'))
(
PARTITION OLD_DATA values LESS THAN (TO_DATE('2022-01-01','YYYY-MM-DD'))
);
/
INSERT into t1 (dt)
with dt (dt, interv) as (
select date '2022-01-01', numtodsinterval(1,'DAY') from dual
union all
select dt.dt + interv, interv from dt
where dt.dt + interv < date '2022-02-01')
select dt from dt;
/
BEGIN
FOR td IN
(
SELECT table_name
, NVL (pr.days, 30) AS days
FROM user_part_tables pt
JOIN user_part_key_columns pkc ON pkc.name = pt.table_name
JOIN user_tab_cols tc USING (table_name, column_name)
LEFT JOIN partition_retention pr USING (table_name)
WHERE pkc.object_type = 'TABLE'
AND pt.partitioning_type = 'RANGE'
AND REGEXP_LIKE (tc.data_type, '^DATE$|^TIMESTAMP.*')
ORDER BY table_name
)
LOOP
drop_partition(
td.table_name,
trunc(sysdate-td.days)
);
END LOOP;
END;
/
Your question says "drop the last partition" which can be different to "drop the only partition"
As stated already in comments, you cannot drop the partition when only one partition is left. If you like to remove the data, then you can TRUNCATE the partition.
This PL/SQL Block should cover all your cases:
CREATE TABLE PARTITION_RETENTION (
TABLE_NAME VARCHAR2(30),
RETENTION INTERVAL DAY(3) TO SECOND(0) --> More generic than number of days
);
DECLARE
CANNOT_DROP_LAST_PARTITION EXCEPTION;
PRAGMA EXCEPTION_INIT(CANNOT_DROP_LAST_PARTITION, -14758);
CANNOT_DROP_ONLY_ONE_PARTITION EXCEPTION;
PRAGMA EXCEPTION_INIT(CANNOT_DROP_ONLY_ONE_PARTITION, -14083);
ts TIMESTAMP;
CURSOR TablePartitions IS
SELECT TABLE_NAME, PARTITION_NAME, p.HIGH_VALUE, t.INTERVAL, RETENTION, DATA_TYPE
FROM USER_PART_TABLES t
JOIN USER_TAB_PARTITIONS p USING (TABLE_NAME)
JOIN USER_PART_KEY_COLUMNS pk ON pk.NAME = TABLE_NAME
JOIN USER_TAB_COLS tc USING (TABLE_NAME, COLUMN_NAME)
JOIN PARTITION_RETENTION r USING (TABLE_NAME);
BEGIN
FOR aPart IN TablePartitions LOOP
EXECUTE IMMEDIATE 'BEGIN :ret := '||aPart.HIGH_VALUE||'; END;' USING OUT ts;
IF ts < SYSTIMESTAMP - aPart.RETENTION THEN
BEGIN
EXECUTE IMMEDIATE 'ALTER TABLE '||aPart.TABLE_NAME||' DROP PARTITION '||aPart.PARTITION_NAME|| ' UPDATE GLOBAL INDEXES';
DBMS_OUTPUT.PUT_LINE('Dropped partittion '||aPart.PARTITION_NAME ||' from table '||aPart.TABLE_NAME);
EXCEPTION
WHEN CANNOT_DROP_ONLY_ONE_PARTITION THEN
DBMS_OUTPUT.PUT_LINE('Cannot drop the only partittion '||aPart.PARTITION_NAME ||' from table '||aPart.TABLE_NAME);
EXECUTE IMMEDIATE 'ALTER TABLE '||aPart.TABLE_NAME||' TRUNCATE PARTITION '||aPart.PARTITION_NAME|| ' UPDATE GLOBAL INDEXES';
DBMS_OUTPUT.PUT_LINE('Truncated partittion '||aPart.PARTITION_NAME ||' from table '||aPart.TABLE_NAME);
WHEN CANNOT_DROP_LAST_PARTITION THEN
BEGIN
DBMS_OUTPUT.PUT_LINE('Drop last partittion '||aPart.PARTITION_NAME ||' from table '||aPart.TABLE_NAME);
EXECUTE IMMEDIATE 'ALTER TABLE '||aPart.TABLE_NAME||' SET INTERVAL ()';
EXECUTE IMMEDIATE 'ALTER TABLE '||aPart.TABLE_NAME||' DROP PARTITION '||aPart.PARTITION_NAME;
EXECUTE IMMEDIATE 'ALTER TABLE '||aPart.TABLE_NAME||' SET INTERVAL( '||aPart.INTERVAL||' )';
EXCEPTION
WHEN CANNOT_DROP_ONLY_ONE_PARTITION THEN
-- Depending on the order the "last" partition can be also the "only" partition at the same time
EXECUTE IMMEDIATE 'ALTER TABLE '||aPart.TABLE_NAME||' SET INTERVAL( '||aPart.INTERVAL||' )';
DBMS_OUTPUT.PUT_LINE('Cannot drop the only partittion '||aPart.PARTITION_NAME ||' from table '||aPart.TABLE_NAME);
EXECUTE IMMEDIATE 'ALTER TABLE '||aPart.TABLE_NAME||' TRUNCATE PARTITION '||aPart.PARTITION_NAME|| ' UPDATE GLOBAL INDEXES';
DBMS_OUTPUT.PUT_LINE('Truncated partittion '||aPart.PARTITION_NAME ||' from table '||aPart.TABLE_NAME);
END;
END;
END IF;
END LOOP;
END;
I have written 1 Stored Procedure where it should create new partition on daily basis, truncate partition & records older than 9 months. Below is the SP-
create or replace procedure sp_partn as
hm varchar2(255);
mm varchar2(50);
yr varchar2(50);
dd varchar2(10);
ym varchar2(50);
dt varchar2(50);
procedure tbl1_prtn as
begin
for i in (select partition_name, high_value from user_tab_partition where table_name='tbl1')
loop
hm := i.high_value;
mm := substr(hm,6,7);
yr := substr(hm,1,4);
dd := substr(hm,9,10);
ym := yr||mm||dd;
dt := to_date(ym,'yyyymmdd');
if sysdate >= add_months(dt, 9) then
execute immediate 'alter table tbl1 drop partition' || i.partition_name;
end if;
end loop;
end tbl1_prtn;
begin
tbl1_prtn;
end sp_partn;
/
When i execute above SP then neither partition create or neither drop partition of older than 9 months. (eg: tbl_20211116)
run
SELECT HIGH_VALUE FROM USER_TAB_PARTITIONS
because for a date partition, the HIGH_VALUE is normally something like "TO_DATE(' 2016-01-04 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'" so I'm not sure your substr commands will work. A better option might be:
hm := i.high_value;
execute immediate 'select '||hm||' from dual' into dt;
which will be immune to string position etc.
If you store dates as dates instead of strings it will make your life much easier and you can use INTERVAL PARTITIONs and this will solve your problem.
SQL> BEGIN
2 FOR cc IN (SELECT partition_name, high_value --
3 FROM user_tab_partitions
4 WHERE table_name = 'TEST_TABLE') LOOP
5 EXECUTE IMMEDIATE
6 'BEGIN
7 IF sysdate >= ADD_MONTHS(' || cc.high_value || ', 2) THEN
8 EXECUTE IMMEDIATE
9 ''ALTER TABLE TEST_TABLE DROP PARTITION '
10 || cc.partition_name || '
11 '';
12 END IF;
13 END;';
14 END LOOP;
15 END;
16 /
I have the following query that returns 1 when executed:
SELECT COUNT(*) FROM TABLE_NAME WHERE Column1='x' AND Column2='y';
In my PL/SQL block, I need to evaluate the condition above in order to execute the business logic. Below is a simplified version:
DECLARE
column1 VARCHAR(20):='x';
column2 VARCHAR(20):='y';
rows_no NUMBER :=0;
sql_query VARCHAR2(200) :='SELECT COUNT(*) FROM TABLE_NAME WHERE Column1=:1 AND Column2=:2';
BEGIN
EXECUTE IMMEDIATE sql_query INTO rows_no USING column1, column2;
DBMS_OUTPUT.PUT_LINE('ROWS NO: '|| rows_no);
END;
The result from the execution of the PL/SQL block is 0 that is different from the result when the query is executed that is 1. I think I abiding by the rules of the binding parameter to query select into in the PL/SQL. I will appreciate any help or guide.
Best Regards,
Rando.
P.S
When I make the modification below :
DECLARE
column1 VARCHAR(20):='x';
column2 VARCHAR(20):='y';
rows_no NUMBER :=0;
sql_query VARCHAR2(200) :='SELECT COUNT(*) FROM TABLE_NAME WHERE Column1=''x'' AND Column2=''y''';
BEGIN
EXECUTE IMMEDIATE sql_query INTO rows_no;
DBMS_OUTPUT.PUT_LINE('ROWS NO: '|| rows_no);
END;
The result is: 1
I have a loop that reads from an excel file and inserts it into the database table(Table_name). The result above is needed to prevent the insertion of dublicates records. The commit is issued at the end of the procedure.
That means that if a record is added from the procedure it remains uncommitted till the procedure finishes.
I doubt this is a problem, uncommitted inserts are not accessible from other connections but in the current connection, they should be accessible. The following has all the simplified logic of the plsql block:
DECLARE;
--declaration variables
BEGIN
LOOP
-- fetching information from excel file
EXECUTE IMMEDIATE sql_query INTO rows_no;
DBMS_OUTPUT.PUT_LINE('ROWS NO: '|| rows_no);
IF rows_no=0 THEN
-- insert the information read from excel in database table
rows_inserted:=rows_inserted+1;
END IF;
END LOOP;
IF rows_inserted>0 THEN
COMMIT;
DBMS_OUTPUT.PUT_LINE('ROWS INSERTED: '||rows_inserted);
ELSE
DBMS_OUTPUT.PUT_LINE('No rows were inserted');
END IF;
END
The problem is most probably outside from Oracle, please try to re-test following script
(The output is shown as comment)
create table tab (column1 varchar2(1), column2 varchar2(1));
set SERVEROUTPUT ON
DECLARE
column1 VARCHAR(20):='x';
column2 VARCHAR(20):='y';
rows_no NUMBER :=0;
sql_query VARCHAR2(200) :='SELECT COUNT(*) FROM TAB WHERE Column1=:1 AND Column2=:2';
BEGIN
EXECUTE IMMEDIATE sql_query INTO rows_no USING column1, column2;
DBMS_OUTPUT.PUT_LINE('ROWS NO: '|| rows_no);
END;
/
-- ROWS NO: 0
insert into tab (column1, column2) values ('x','y');
DECLARE
column1 VARCHAR(20):='x';
column2 VARCHAR(20):='y';
rows_no NUMBER :=0;
sql_query VARCHAR2(200) :='SELECT COUNT(*) FROM TAB WHERE Column1=:1 AND Column2=:2';
BEGIN
EXECUTE IMMEDIATE sql_query INTO rows_no USING column1, column2;
DBMS_OUTPUT.PUT_LINE('ROWS NO: '|| rows_no);
END;
/
-- ROWS NO: 1
I have a table which has Partitions and subpartitions. I have to first filter the results of Partition's High_Value (TO_DATE(' 2020-03-29 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')) with less than some date value and then in the result set filter with Subpartion High value with some text value and if the condition satisfy's then drop that sub partition. I have written the code until given below, but not sure how to proceed further. Can someone please help on how to declare d_tmp and then loop through each subpartion and check
DECLARE
CURSOR get_parts IS
select partition_name, high_value
from ALL_TAB_PARTITIONS
where table_name = 'TempTable';
l_tmp LONG;
d_tmp DATE;
BEGIN
FOR part_rec IN get_parts
LOOP
l_tmp := part_rec.high_value;
EXECUTE IMMEDIATE 'SELECT ' || SUBSTR(l_tmp, 1, 90) || ' FROM DUAL' INTO d_tmp;
DBMS_OUTPUT.PUT_LINE( to_char(d_tmp, 'DD-MM-YYYY'));
END LOOP;
END;
You are most of the way there. You just loop around the sub partitions for that partition
DECLARE
CURSOR get_parts IS
select partition_name, high_value
from ALL_TAB_PARTITIONS
where table_name = 'TEMPTABLE';
l_tmp LONG;
d_tmp DATE;
l_tmp2 LONG;
BEGIN
FOR part_rec IN get_parts
LOOP
l_tmp := part_rec.high_value;
EXECUTE IMMEDIATE 'SELECT ' || SUBSTR(l_tmp, 1, 90) || ' FROM DUAL' INTO d_tmp;
DBMS_OUTPUT.PUT_LINE( to_char(d_tmp, 'DD-MM-YYYY'));
for i in (
select subpartition_name, high_value
from ALL_TAB_SUBPARTITIONS
where table_name = 'TEMPTABLE'
and partition_name = get_parts.partition_name
)
loop
l_tmp2 := i.high_value;
--
-- your checks
--
if [checks passed] then
execute immediate 'alter table TEMPTABLE drop subpartition '||i.subpartition_name;
end loop;
END LOOP;
END;