Oracle rename partition if exists - sql

I need to create a statement that can be run to rename a partition only if specific partition name exists and if not then continue on to execute other code.
basic command = ALTER TABLE TEST RENAME PARTITION P1 TO P2:
I have looked at the following but have not come up with a solution
Using IF ELSE in Oracle
https://docs.oracle.com/cd/B28359_01/appdev.111/b28370/controlstructures.htm

You could check whether the partition exists within the table USER_TAB_PARTITIONS:
DECLARE
v_p1_exists AS NUMBER;
v_p2_exists AS NUMBER;
BEGIN
SELECT COUNT(*)
INTO v_p1_exists
FROM user_tab_partitions
WHERE table_name = 'TEST'
AND partition_name = 'P1';
SELECT COUNT(*)
INTO v_p2_exists
FROM user_tab_partitions
WHERE table_name = 'TEST'
AND partition_name = 'P2';
IF (v_p1_exists <> 0) AND (v_p2_exists = 0) THEN
EXECUTE IMMEDIATE 'ALTER TABLE TEST RENAME PARTITION P1 TO P2';
END;
END;

I depends on your requirements but a basic procedure would be this one:
DECLARE
PARTITION_DOES_NOT_EXIST EXCEPTION;
PRAGMA EXCEPTION_INIT(PARTITION_DOES_NOT_EXIST, -2149);
BEGIN
BEGIN
EXECUTE IMMEDIATE 'ALTER TABLE TEST RENAME PARTITION P1 TO P2';
EXCEPTION
WHEN PARTITION_DOES_NOT_EXIST THEN NULL;
END;
... ohter commands
END;

One option is to enclose ALTER TABLE into its own BEGIN-END block, with appropriate exception handling section. Mine is kind of stupid (WHEN OTHERS, eh?); feel free to rewrite it to be meaningful - it is just to show how to do it.
So: if ALTER TABLE fails, it'll raise some error; it'll be captured, handled, and code will continue its execution.
begin
begin
execute immediate 'alter table test rename partition p1 to p2';
exception
when others then
-- ignore errors
null;
end;
-- continue to execute other code
...
end;

Related

Delete rows only if the table exists

I need to run a delete statement on my database if a table was previously created.
The problem is - I can't just run the delete statement, since the product is not on every client's productive environments - therefore, they don't have the table where I want to run the delete statement, and would end up with an error 00942. 00000 - "table or view does not exist".
An example:
I would like to run something like this:
IF EXISTS (TABLE TB_FIELD)
DELETE FROM TB_FIELD WHERE ID = '213';
If there isn't a generic statement, I would like one that would run for Oracle databases
Here's one for Oracle. This assumes the current user owns the table. If you're updating someone else's table you'll need to swap out user_tables with dba_tables.
declare
table_name_l user_tables.table_name%type;
begin
select table_name into table_name_l from user_tables where table_name = 'TB_FIELD';
-- if we didn't raise an exception the table must exist
execute immediate 'delete from tb_field where id = :1' using '213';
exception
when no_data_found then
-- do nothing - table doesn't exist
null;
end;
Simplest way is to catch and ignore the "table not found" exception:
declare
l_id number := 12345;
begin
execute immediate 'delete tb_field where id=:1' using l_id;
exception
when others then
if sqlcode != -942 /*table or view does not exist*/ then
raise;
end if;
end;
/

Oracle procedure/function to create a trigger in table

I'm trying to create a procedure that given a table name, it will create a sequence and auto incrementing trigger, all using variables based on the table name.
Code :
CREATE OR REPLACE procedure CREATE_SEQUENTIAL_TR(table_name VARCHAR)
is -- Tried using declare but it wouldn't accept
coluna_cod varchar(100 char);
begin
--Finding the cod column name for this table first
--They start with "PK_CD"
select
COLUMN_NAME
into
coluna_cod
from
ALL_TAB_COLUMNS
where
TABLE_NAME=table_name
and COLUMN_NAME like "PK_CD%";
--Creating the sequence obj
drop sequence "cod" || table_name;
create sequence "cod" || table_name;
--Now creating the trigger
create or replace trigger "cod" || table_name || "tr"
before
UPDATE or INSERT on table_name
for each row
declare
cod number := coluna_cod;
tr_name varchar(100 char) := "cod" || table_name
begin
if UPDATING then
if :new.cod != :old.cod then
:new.cod := :old.cod;
end if;
else -- inserting
:new.cod := tr_name.nextval();
end if;
end;
end;
The complexity of this ended up quite out of the scope of my knowledge.
At the moment it is giving an error on drop sequence "cod" || table_name (Unexpected DROP symbol found) but I'm sure I have made other errors.
Can someone help me figure this logic out?
You can't put DDL statements (like drop or create or alter) directly inside a PL/SQL block. If you want to do DDL inside PL/SQL, you can do an execute immediate:
declare
begin
drop sequence X; -- error
execute immediate 'drop sequence X'; -- works fine
end;
/

Oracle SQL - If Exists, Drop Table & Create

Can some one please guide me what's wrong with this query? In SQL Server we just check the presence of the Object_ID of a table to drop it and re-create it. I am new to Oracle and wrote this query:
declare Table_exists INTEGER;
BEGIN
Select count(*) into Table_exists from sys.all_tables where table_name='TABLENAME1';
EXCEPTION
WHEN NO_DATA_FOUND
THEN
Table_Exists :=0;
if(table_exists)=1
Then
Execute Immediate 'Drop Table TABLENAME1;'
'Create Table TABLENAME1;';
DBMS_OUTPUT.PUT_LINE('Table Dropped and Re-Created!');
Else
Execute Immediate 'Create Table TABLENAME1;';
DBMS_OUTPUT.PUT_LINE('New Table Created!');
END IF;
END;
I get the output - ANONYMOUS BLOCK COMPLETED, but the table is not created. The table was previously existing, so I dropped it to check if the PL/SQL is actually creating the table, but NO. What is wrong here? What am I missing? Please guide.
When you are using all_tables filter the results for your
schema by adding where owner = 'your_schema'
or use sys.user_tables
ALL_TABLES describes the relational tables accessible to the current user
USER_TABLES describes the relational tables owned by the current user.
When use execute_emmidiate remove the ; from the query;
Modified query;
DECLARE
Table_exists INTEGER;
BEGIN
Select count(*) into Table_exists from sys.user_tables where table_name='TABLENAME1';
--or
--Select count(*) into Table_exists from sys.all_tables
--where table_name='TABLENAME1' and owner = 'your_DB';
if table_exists = 1 Then
Execute Immediate 'Drop Table TABLENAME1';
Execute Immediate 'Create Table TABLENAME1(num number)';
DBMS_OUTPUT.PUT_LINE('Table Dropped and Re-Created!');
Else
Execute Immediate 'Create Table TABLENAME1(num number)';
DBMS_OUTPUT.PUT_LINE('New Table Created!');
END IF;
END;
First note:
Select count(*) into Table_exists
from sys.all_tables
where table_name = 'TABLENAME1';
will always return one row. You don't need the exception handling.
My best guess is that you have more than one table called TABLENAME1. Run this query to find out:
Select *
from sys.all_tables
where table_name = 'TABLENAME1';
Oracle stores tables from all owners that you can access. You might also want to check OWNER_NAME in the where clause.
However, you seem to understand exception handling. So, just drop the table, ignore any errors, and then recreate the table.
The EXCEPTION clause lasts till the next END and not just the next statement. If you want to continue after catching the exception you need to add an additional BEGIN/END:
declare
Table_exists INTEGER;
BEGIN
BEGIN
Select count(*) into Table_exists from sys.all_tables where table_name='TABLENAME1';
EXCEPTION
WHEN NO_DATA_FOUND THEN
Table_Exists :=0;
END;
if(table_exists)=1 Then
Execute Immediate 'Drop Table TABLENAME1;'
Execute Immediate 'Create Table TABLENAME1;';
DBMS_OUTPUT.PUT_LINE('Table Dropped and Re-Created!');
Else
Execute Immediate 'Create Table TABLENAME1;';
DBMS_OUTPUT.PUT_LINE('New Table Created!');
END IF;
END;
As pointed out by Gordon, the EXCEPTION clause is not really needed in this case since count(*) will always return one row. So the following is sufficient:
declare
Table_exists INTEGER;
BEGIN
Select count(*) into Table_exists from sys.all_tables where table_name='TABLENAME1';
if(table_exists)=1 Then
Execute Immediate 'Drop Table TABLENAME1;'
Execute Immediate 'Create Table TABLENAME1;';
DBMS_OUTPUT.PUT_LINE('Table Dropped and Re-Created!');
Else
Execute Immediate 'Create Table TABLENAME1;';
DBMS_OUTPUT.PUT_LINE('New Table Created!');
END IF;
END;

checking if table exists function

Can you help me rectify this block of code plz?
`CREATE OR REPLACE FUNCTION TABLE_EXISTS(name VARCHAR(50))
RETURNS BOOLEAN
AS
BEGIN
DECLARE counttable INTEGER;
SELECT COUNT(1) INTO counttable FROM USER_TABLES WHERE TABLE_NAME=name;
if counttable=0 then
return false
else
return true
end if;
END;
/
IF (TABLE_EXISTS("LEADS_DELETED")) then
DROP TABLE LEADS_DELETED;
end if;
/
CREATE GLOBAL TEMPORARY TABLE LEADS_DELETED
(
ID NUMBER(19),
PRIMARY KEY (ID)
) ON COMMIT DELETE ROWS`
You can use a construct like this when you want to create or recreate a table (try to drop and catch the ORA-00942 exception that gets thrown when the object doesn't exist):
DECLARE
table_does_not_exist exception;
pragma exception_init(table_does_not_exist, -942);
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE LEADS_DELETED';
EXCEPTION
WHEN table_does_not_exist THEN
NULL;
END;
/
CREATE TABLE ...
You have double quotes aroung your table name "LEADS_DELETED" should be 'LEADS_DELETED'.
I'd also wrap the table_name in your query with UPPER(table_name) too.
You also need to put the DROP TABLE command inside an EXECUTE IMMEDIATE wrapper.
You declare your variable in the wrog place too, it needs to be declared before the BEGIN clause.
CREATE OR REPLACE
FUNCTION TABLE_EXISTS(name VARCHAR(50))
RETURNS BOOLEAN
AS
counttable INTEGER;
BEGIN
SELECT COUNT(1)
INTO counttable
FROM USER_TABLES
WHERE TABLE_NAME=UPPER(name);
if counttable=0
then
return false
else
return true
end if;
END;
/
-- I suggest you use a bind variable instead of the literal table name.
IF TABLE_EXISTS('LEADS_DELETED')
THEN
EXECUTE IMMEDIATE 'DROP TABLE LEADS_DELETED';
END IF;
/
-- Could be
IF table_exists(v_table_name)
THEN
EXECUTE IMMEDIATE 'DROP TABLE :tablename'
USING v_table_name;
END IF;

sql exception handling

CREATE OR REPLACE PROCEDURE p_createLocaltable
IS
table_already_exist EXCEPTION;
PRAGMA EXCEPTION_INIT (table_already_exist, -00955);
BEGIN
create table local_table as
select * from supplied_table
where rownum < 1;
EXCEPTION
when table_already_exist then
DBMS_OUTPUT.put_line('Table already exists , does not need to recreate it');
END;
can anyone see any problem of the above code?
You cannot do DDL in a PL/SQL block like that. You'll need to use execute immediate.
You would need to do it like this
CREATE OR REPLACE PROCEDURE p_createLocaltable
IS
table_already_exist EXCEPTION;
PRAGMA EXCEPTION_INIT (table_already_exist, -00955);
BEGIN
execute immediate 'create table local_objects as select * from all_objects where 1=0';
EXCEPTION
when table_already_exist then
DBMS_OUTPUT.put_line('Table already exists , does not need to recreate it');
END;
Check the orafaq page on this