I had created a lot of tables and users while testing some SQL command and now I dont remember exactly all the tables and user's name.
But now I want to delete all of it for my big project.So is it possible to delete it in SQL command line ?
This PL/SQL block will be useful to delete all the data in oracle data base
BEGIN
FOR cur_rec IN (SELECT object_name, object_type
FROM user_objects
WHERE object_type IN
('TABLE',
'VIEW',
'PACKAGE',
'PROCEDURE',
'FUNCTION',
'SEQUENCE',
'SYNONYM',
'PACKAGE BODY'
))
LOOP
BEGIN
IF cur_rec.object_type = 'TABLE'
THEN
EXECUTE IMMEDIATE 'DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '" CASCADE CONSTRAINTS';
ELSE
EXECUTE IMMEDIATE 'DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '"';
END IF;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ( 'FAILED: DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '"'
);
END;
END LOOP;
END;
/
execute this command:
BEGIN
FOR table_ IN (SELECT * FROM dba_tables where owner like 'YOUR_SCHEMA') LOOP
execute immediate 'truncate table ' || table_.owner || '.' || table_.table_name ||' cascade';
END LOOP;
END;
/
BEGIN
FOR T in (SELECT table_name FROM user_tables) LOOP
EXECUTE IMMEDIATE 'ALTER TABLE '||T.table_name||' DISABLE ALL CONSTRAINTS';
--This will disable all the constraint
END LOOP;
FOR T in (SELECT table_name FROM user_tables) LOOP
EXECUTE IMMEDIATE 'TRUNCATE TABLE '||T.table_name;
END LOOP;
FOR T in (SELECT table_name FROM user_tables) LOOP
EXECUTE IMMEDIATE 'ALTER TABLE '||T.table_name||' ENABLE ALL CONSTRAINTS';
END LOOP;
END;
Alter table ... disable all constraints throws a ORA-01735: invalid ALTER TABLE option if there is no constraint defined for the table, which would cause the script to fail to truncate every table if there is at least one table without constraint. You might want to place the execute immediate within a begin -exception block
You have two ways:
you can use function delete instead truncate for this function you should not disable constraints, but it work more slowly because you can rollback this operation if you will go this way:
BEGIN
FOR table_ IN (SELECT * FROM dba_tables where owner like 'YOUR_SCHEMA') LOOP
execute immediate 'delete from table ' || table_.owner || '.' || table_.table_name;
END LOOP;
END;
use truncate it is more faster, but you must diable constraints on table:
begin
for disable_constraint_ in
(select * from dba_constraints where owner= 'YOUR_SCHEMA'
)
loop
execute immediate 'alter table ' || disable_constraint_.owner || '.' || disable_constraint_.table_name ||' disable constraint '|| disable_constraint_.constraint_name;
end loop;
for table_ in (select * from dba_tables where owner = 'YOUR_SCHEMA')
loop
execute immediate 'truncate table ' || table_.owner || '.' ||table_.table_name ||' cascade';
end loop;
for enable_constaint_ in (select * from dba_constraints where owner= 'YOUR_SCHEMA')
loop
execute immediate 'alter table ' || enable_constaint_.owner || '.' || enable_constaint_.table_name ||' enable constraint '|| enable_constaint_.constraint_name;
end loop;
end;
Related
I have a query that works without errors, but if I try to insert data into a temporary table, then it is empty.
What am I doing wrong?
DO $$DECLARE r record;
begin
FOR r IN select concat('select * from ',schema_name,'.etl_load_log') as sql_str, schema_name
from information_schema.schemata s
where schema_name like '%db_tmd'
and rtrim(ltrim( schema_name,'disd_'),'_db_tmd') <> ''
and rtrim(ltrim( schema_name,'disd_'),'_db_tmd') like '%69%'
LOOP
EXECUTE 'SELECT * FROM ' || quote_ident(r.schema_name) || '.' || 'etl_load_log';
END LOOP;
END$$;
select * from result_table
--EXECUTE 'insert into result_table (package_name, info_system, inst_cd text SELECT * FROM ' || quote_ident(r.schema_name) || '.' || 'etl_load_log';
replace string:
EXECUTE 'insert into result_table (package_name, info_system, inst_cd text SELECT * FROM ' || quote_ident(r.schema_name) || '.' || 'etl_load_log';
I have an oracle database and I am running the below query
select table_name
from db_test.test
where table_name like '%20170128'
This returns me a column with all the tables with the specific date at the end.
How can I take this list and query them?
You'd need dynamic SQL. If you're running a simple query against each table (I'm just doing a count(*) in this example), something like this would work
declare
l_cnt integer;
l_sql varchar2(1000);
begin
for t in (select table_name
from db_test.test
where table_name like '%20170128')
loop
l_sql := 'select count(*) from ' || t.table_name;
execute immediate l_sql
into l_cnt;
dbms_output.put_line( t.table_name || ' has ' || l_cnt || ' rows.' );
end loop;
end;
postgesql 9.6.17
There are result rows from Renaming multiple columns in PostgreSQL
they contain some commands like
alter table .....
alter table .....
how to immediately exec them in sql like
SELECT
EXEC SQL 'ALTER TABLE ' || tab_name || ' RENAME COLUMN '
|| quote_ident(column_name) || ' TO '
|| lower(quote_ident( column_name)) || ';' commit
FROM (
SELECT
quote_ident(table_schema) || '.' || quote_ident(table_name) as tab_name,
column_name
FROM information_schema.columns
WHERE
table_schema = 'public'
) sub;
but example ↑ fails
You can use the DO statement for executing this. Something like given below:
DO $$
DECLARE rec TEXT;
BEGIN
FOR r in SELECT
'ALTER TABLE ' || tab_name || ' RENAME COLUMN '
|| quote_ident(column_name) || ' TO '
|| lower(quote_ident( column_name))
FROM (
SELECT
quote_ident(table_schema) || '.' ||
quote_ident(table_name) as tab_name,
column_name
FROM information_schema.columns
WHERE
table_schema = 'public'
) sub
LOOP
EXECUTE rec;
END LOOP;
END $$;
For more detail refer the following link:
https://www.postgresql.org/docs/9.6/sql-do.html
This was what i was trying
SET SERVEROUTPUT ON;
DECLARE
sql_query VARCHAR2(32767);
BEGIN
FOR t IN (SELECT table_name, column_name FROM user_tab_columns)
LOOP
EXECUTE IMMEDIATE sql_query := 'SELECT * FROM ' || t.table_name ;
END LOOP;
END;
This query gives you the number of distinct values per column (assuming that statistics are up to date).
select owner, table_name, column_name, num_distinct
from all_tab_col_statistics
Maybe this would be enough.
If you need to have the distinct values, you have to modify the sql_query param in your script as follows:
EXECUTE IMMEDIATE sql_query := 'SELECT distinct '|| t.column_name ||
' FROM ' || t.table_name ;
I put together a script that drops triggers, tables, indexes, and sequences.
BEGIN
/*Delete All Triggers */
FOR i in (select trigger_name,owner
from dba_triggers ) LOOP
EXECUTE IMMEDIATE 'DROP TRIGGER '||i.owner||'.'||i.trigger_name;
END LOOP;
/*Delete All Indexes*/
FOR ind IN
(SELECT index_name FROM user_indexes WHERE table_name = 'my_table' AND index_name NOT IN
(SELECT unique index_name FROM user_constraints WHERE
table_name = 'my_table' AND index_name IS NOT NULL))
LOOP
execute immediate 'DROP INDEX '||ind.index_name;
END LOOP;
/*Delete All Tables*/
FOR c IN (SELECT table_name FROM user_tables) LOOP
EXECUTE IMMEDIATE ('DROP TABLE ' || c.table_name || ' CASCADE CONSTRAINTS');
END LOOP;
/*Delete all Sequences*/
FOR s IN (SELECT sequence_name FROM user_sequences) LOOP
EXECUTE IMMEDIATE ('DROP SEQUENCE ' || s.sequence_name);
END LOOP;
END;
I was wondering if there is specific order I should do it? Is there anything else I need to consider?