Skip to content

Instantly share code, notes, and snippets.

@anton-x-t
Last active October 11, 2018 11:40
Show Gist options
  • Save anton-x-t/37de1a0717d44c4c93ea21792eb3aca9 to your computer and use it in GitHub Desktop.
Save anton-x-t/37de1a0717d44c4c93ea21792eb3aca9 to your computer and use it in GitHub Desktop.
oracle procedure to create table without knowing if table exists or not, tested in Oracle Database 11g
create or replace procedure your_table_name_dc_proc is
-- In the procedure name, dc is short for drop and create.
-- On getting advices and tips, thank you very much StackOverflow, stackoverflow.com
-- License for this code: cc by-sa 3.0
table_name varchar2(50);
procedure drop_table(table_name in varchar2) is
begin
-- On dropping a table not knowing if the table exists or not, thank you Jeffrey Kemp, https://stackoverflow.com/q/1799128
-- Drop table.
execute immediate 'drop table ' || table_name;
end drop_table;
procedure create_table(table_name in varchar2) is
begin
-- Create table.
execute immediate 'create table ' || table_name || '
(
col1 type1,
col2 type2,
***...***
)
***custom settings***';
end create_table;
begin
-- On executing procedure, thank you Thorsten, https://stackoverflow.com/q/1854427
table_name := 'your_table_name';
drop_table(table_name);
create_table(table_name);
exception
when others then
--Catch and raise anything other except drop table fail.
if sqlcode != -942 then
raise;
end if;
create_table(table_name);
end your_table_name_dc_proc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment