drop all tables sharing the same prefix in postgres - sql

I would like to delete all tables sharing the same prefix ('supenh_agk') from the same database, using one sql command/query.

To do this in one command you need dynamic SQL with EXECUTE in a DO statement (or function):
DO
$do$
DECLARE
_tbl text;
BEGIN
FOR _tbl IN
SELECT quote_ident(table_schema) || '.'
|| quote_ident(table_name) -- escape identifier and schema-qualify!
FROM information_schema.tables
WHERE table_name LIKE 'prefix' || '%' -- your table name prefix
AND table_schema NOT LIKE 'pg\_%' -- exclude system schemas
LOOP
RAISE NOTICE '%',
-- EXECUTE
'DROP TABLE ' || _tbl; -- see below
END LOOP;
END
$do$;
This includes tables from all schemas the current user has access to. I excluded system schemas for safety.
If you do not escape identifiers properly the code fails for any non-standard identifier that requires double-quoting.
Plus, you run the risk of allowing SQL injection. All user input must be sanitized in dynamic code - that includes identifiers potentially provided by users.
Potentially hazardous! All those tables are dropped for good. I built in a safety. Inspect the generated statements before you actually execute: comment RAISE and uncomment the EXECUTE.
If any other objects (like views etc.) depend on a table you get an informative error message instead, which cancels the whole transaction. If you are confident that all dependents can die, too, append CASCADE:
'DROP TABLE ' || _tbl || ' CASCADE;
Closely related:
Update column in multiple tables
Changing all zeros (if any) across all columns (in a table) to... say 1
Alternatively you could build on the catalog table pg_class, which also provides the oid of the table and is faster:
...
FOR _tbl IN
SELECT c.oid::regclass::text -- escape identifier and schema-qualify!
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname NOT LIKE 'pg\_%' -- exclude system schemas
AND c.relname LIKE 'prefix' || '%' -- your table name prefix
AND c.relkind = 'r' -- only tables
...
System catalog or information schema?
How to check if a table exists in a given schema
How does c.oid::regclass defend against SQL injection?
Table name as a PostgreSQL function parameter
Or do it all in a single DROP command. Should be a bit more efficient:
DO
$do$
BEGIN
RAISE NOTICE '%', (
-- EXECUTE (
SELECT 'DROP TABLE ' || string_agg(format('%I.%I', schemaname, tablename), ', ')
-- || ' CASCADE' -- optional
FROM pg_catalog.pg_tables t
WHERE schemaname NOT LIKE 'pg\_%' -- exclude system schemas
AND tablename LIKE 'prefix' || '%' -- your table name prefix
);
END
$do$;
Related:
Is there a postgres command to list/drop all materialized views?
Using the conveniently fitting system catalog pg_tables in the last example. And format() for convenience. See:
How to check if a table exists in a given schema
Table name as a PostgreSQL function parameter

Suppose the prefix is 'sales_'
Step 1: Get all the table names with that prefix
SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'sales_%';
Step 2: Click the "Download as CSV" button.
Step 3: Open the file in an editor and replace "sales_ with ,sales
and " with a space
Step 4: DROP TABLE sales_regist, sales_name, sales_info, sales_somthing;

This is sql server command, can you try this one, is it worked in postgres or not.
This query wil generate the sql script for delete
SELECT 'DROP TABLE "' || TABLE_NAME || '"'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '[prefix]%'
[EDIT]
begin
for arow in
SELECT 'DROP TABLE "' || TABLE_NAME || '"' as col1
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '[prefix]%'
LOOP
--RAISE NOTICE '%',
EXECUTE 'DROP TABLE ' || arow ;
END LOOP;
end;

Related

COMMENT ON generates ORA-00905: missing keyword through EXECUTE IMMEDIATE

I need help on figuring out the problem with the ORA-00905: missing keyword ORA-06512: at line 73
When it says line 73 it actually refers to the sql statement itself at line 56. However, I am using this same script with a different table which working perfectly.
By changing the schema, table and column name I keep getting this error. I have be experimenting with several versions and also using fetch into cursor.
It keeps saying the sql statement has missing keyword but it is working on another script with the same line. I am hoping somebody could help me here. This is my first time posting on this forum and I am hoping someday I could contribute to this great community. Thank you in advance!
DECLARE
--CREATE OR REPLACE PROCEDURE setcomment
--IS
CURSOR cur IS
SELECT COLUMN_NAME, TABLE_NAME, OWNER
FROM DBA_TAB_COLUMNS
WHERE COLUMN_NAME = 'SSAN'
ORDER BY OWNER ASC, TABLE_NAME ASC, COLUMN_NAME ASC;
c_schema_name DBA_TAB_COLUMNS.OWNER%type;
c_table_name DBA_TAB_COLUMNS.TABLE_NAME%type;
c_column_name DBA_TAB_COLUMNS.COLUMN_NAME%type;
--This is a variable name to concatenate column names from <c_schema_name>.<c_table_name>.<c_column_name>
col_name VARCHAR(250) ;
--This is a variable to hold SQL statement and the message to be commented
sql_stmt1 VARCHAR(2000) ;
msg VARCHAR(250) := ' '' Comment going here '' ';
BEGIN
--Looping r cursor through cur cursor. Retrieving a row of record at a time
FOR r in cur LOOP
c_schema_name := r.owner;
c_table_name := r.table_name;
c_column_name := r.column_name;
--Concatenate all the column names into a single column name.
col_name := c_schema_name||'.'||c_table_name||'.'||c_column_name;
sql_stmt1 := 'COMMENT ON COLUMN '|| col_name ||' IS ''Comment going here '' ' ;
-- sql_stmt1 := 'COMMENT ON COLUMN '|| col_name ||' IS '||msg;
EXECUTE IMMEDIATE sql_stmt1;
--EXECUTE IMMEDIATE 'COMMENT ON COLUMN '|| c_schema_name||'.'||c_table_name||'.'||c_column_name || ' IS '' Comment going here '' ' ;
DBMS_OUTPUT.PUT_LINE ('COMMENT ON ' || col_name || ' procedure completed....');
END LOOP;
END;
/
If you still cannot find a source of the error, then create a log table, run the below code, and display (select) all error entries from the table
Then try to manually run the command.
Does you user have an appriopriate privileges to comment on tables in other schemas ? It can have a privilege to SELECT from DBA_TAB_COLS, but that doesn't mean that it can modify other schemas/tables.
CREATE TABLE log_errors( error_msg varchar2(4000));
DECLARE
CURSOR cur IS
SELECT COLUMN_NAME, TABLE_NAME, OWNER
FROM DBA_TAB_COLUMNS
WHERE COLUMN_NAME = 'SSAN'
ORDER BY OWNER ASC, TABLE_NAME ASC, COLUMN_NAME ASC;
col_name VARCHAR(250) ;
sql_stmt1 VARCHAR(2000) ;
msg VARCHAR(250) := 'Comment going here';
BEGIN
FOR r in cur LOOP
col_name := '"'|| r.OWNER ||'"."'||r.TABLE_NAME||'"."'||r.COLUMN_NAME||'"';
sql_stmt1 := 'COMMENT ON COLUMN ' || col_name || ' IS ''' || msg || '''';
BEGIN
EXECUTE IMMEDIATE sql_stmt1;
EXCEPTION
WHEN OTHERS THEN
INSERT INTO log_errors( error_msg ) VALUES ( sql_stmt1 );
END;
END LOOP;
END;
/
SELECT * FROM log_errors;
In addition to Mathguy's answer - your script will fail if any of the tables has been created using quoted identifiers
Database Object Naming Rules
Every database object has a name. In a SQL statement, you represent
the name of an object with a quoted identifier or a nonquoted
identifier.
A quoted identifier begins and ends with double quotation marks (").
If you name a schema object using a quoted identifier, then you must
use the double quotation marks whenever you refer to that object.
A nonquoted identifier is not surrounded by any punctuation.
You can use either quoted or nonquoted identifiers to name any
database object. However, database names, global database names, and
database link names are always case insensitive and are stored as
uppercase. If you specify such names as quoted identifiers, then the
quotation marks are silently ignored.
Simple practical example - a name of the first table is nonquoted identifier, a name of the second table is quoted identifier :
CREATE TABLE table_one (
SSAN int
);
CREATE TABLE "TaBle ##% TWO" (
SSAN int
);
SELECT 'COMMENT ON COLUMN ' || OWNER || '.' || TABLE_NAME || '.' || COLUMN_NAME || ' IS ''My superb comment'''
As my_comment_command
FROM ALL_TAB_COLUMNS
WHERE COLUMN_NAME = 'SSAN' ;
MY_COMMENT_COMMAND
----------------------------------------------------------------
COMMENT ON COLUMN SCOTT.TABLE_ONE.SSAN IS 'My superb comment'
COMMENT ON COLUMN SCOTT.TaBle ##% TWO.SSAN IS 'My superb comment'
It's obvious, that the second command will fail.
But if you use quotes in your script, then everything will work fine:
SELECT 'COMMENT ON COLUMN "' || OWNER || '"."' || TABLE_NAME || '"."' || COLUMN_NAME || '" IS ''My superb comment'''
As my_comment_command
FROM ALL_TAB_COLUMNS
WHERE COLUMN_NAME = 'SSAN' ;
MY_COMMENT_COMMAND
----------------------------------------------------------------------
COMMENT ON COLUMN "SCOTT"."TABLE_ONE"."SSAN" IS 'My superb comment'
COMMENT ON COLUMN "SCOTT"."TaBle ##% TWO"."SSAN" IS 'My superb comment'

ERROR at line 1: ORA-00911: invalid character ORA-06512: at line 17

I am not a frequent user of database & once in a while i need to create/run/execute a few PL/SQL blocks. I have a similar situation right now, where I have the below block which while executing as SYS as SYSDBA in oracle database user throws the error :-
DECLARE
*
ERROR at line 1:
ORA-00911: invalid character
ORA-06512: at line 17
The PL/SQL Block is as below :-
DECLARE
TYPE RefCurTyp IS REF CURSOR;
alter_tbl VARCHAR2(200);
a_null CHAR(1);
tbl VARCHAR2(200);
clmn VARCHAR2(200);
dtyp VARCHAR2(200) ;
dlth VARCHAR2(200);
c RefCurTyp;
BEGIN
open c for 'select utc.table_name, utc.column_name, utc.data_type, utc.data_length FROM user_tab_columns utc, user_tables ut
WHERE utc.data_type = ''VARCHAR2'' AND utc.char_used =''B'' AND ut.table_name = utc.table_name';
LOOP
dbms_output.put_line(clmn);
FETCH c INTO tbl, clmn, dtyp, dlth;
EXIT WHEN c%NOTFOUND;
EXECUTE IMMEDIATE
'alter table '||tbl||' modify ('||clmn||' '||dtyp||'('||dlth||' CHAR))';
END LOOP;
CLOSE c;
END;
Even after pounding my head on it for 3 days i am unable to figure out the issue with this. Any input is appreciated.
While executing the same code via TOAD i get :-
You can use dbms_output to display the dynamic statement you are executing. To make sure you see and execute the same thing it's simpler to put the statement into a variable (you have one you aren't using). If you change the cursor type you don't need the local variables though, you can construct the statement as part of the cursor query, and then refer to it multiple times; you also won't have to escape your single quotes:
set serveroutput on
BEGIN
FOR r IN (
SELECT 'alter table ' || utc.table_name || ' modify (' || utc.column_name || ' '
|| utc.data_type || '(' || utc.data_length || ' CHAR))' as alter_stmt
FROM user_tab_columns utc
JOIN user_tables ut ON ut.table_name = utc.table_name
WHERE utc.data_type = 'VARCHAR2' AND utc.char_used ='B'
)
LOOP
dbms_output.put_line(r.alter_stmt);
execute immediate r.alter_stmt;
END LOOP;
END;
/
I suspect you have a table or column name that contains an invalid character and was created with a quoted identifier. That will probably be obvious from the output you see immediately before it fails. You can easily add double quotes to all of the identifiers by concatenating them as part of the statement generation:
BEGIN
FOR r IN (
SELECT 'alter table "' || utc.table_name || '" modify ("' || utc.column_name || '" '
|| utc.data_type || '(' || utc.data_length || ' CHAR))' as alter_stmt
FROM user_tab_columns utc
JOIN user_tables ut ON ut.table_name = utc.table_name
WHERE utc.data_type = 'VARCHAR2' AND utc.char_used ='B'
)
LOOP
dbms_output.put_line(r.alter_stmt);
execute immediate r.alter_stmt;
END LOOP;
END;
/
You said you're running this while connected as SYS, and you're looking at user_tables, so you are altering tables owned by SYS - which seems like a very bad idea. Even if you don't intend to modify built-in data dictionary tables, this will do so, and that would imply you've been creating your own objects in the SYS schema - which is generally considered a very bad idea. You should create a separate user account and only create and modify objects in that schema.
In my 11g instance SYS has a table that generates output from my first query as:
alter table _default_auditing_options_ modify (A VARCHAR2(1 CHAR));
... which would get ORA-00911 because of the underscores. If the identifiers are quoted then it would work:
alter table "_default_auditing_options_" modify ("A" VARCHAR2(1 CHAR));
... but, once again, you should not be altering built-in tables.

Postgres - Run same query on all databases (same schemas)

hoping this is a pretty straightforward question.
I have a straightforward SELECT query (with a few sub-queries built in). I have over 40 DBs and I need to run this query for all DBs (all have same schema) and return the result in a big table.
I'm imagining a loop sequence (like with javascript's i=0; i < 40; i++) with a defined variable that will automatically stop once it's run all the DBs.
(I am working in Navicat, tho that probably doesn't matter)
Thank you!
In case someone needs a more involved example on how to do cross-database queries, here's an example that cleans up the databasechangeloglock table on every database that has it:
CREATE EXTENSION IF NOT EXISTS dblink;
DO
$$
DECLARE database_name TEXT;
DECLARE conn_template TEXT;
DECLARE conn_string TEXT;
DECLARE table_exists Boolean;
BEGIN
conn_template = 'user=myuser password=mypass dbname=';
FOR database_name IN
SELECT datname FROM pg_database
WHERE datistemplate = false
LOOP
conn_string = conn_template || database_name;
table_exists = (select table_exists_ from dblink(conn_string, '(select Count(*) > 0 from information_schema.tables where table_name = ''databasechangeloglock'')') as (table_exists_ Boolean));
IF table_exists THEN
perform dblink_exec(conn_string, 'delete from databasechangeloglock');
END IF;
END LOOP;
END
$$
It is possible to accomplish this in a single query using an Postgres' extension called dblink. This extension becomes available after you install postgresql-contrib package.
To be able to access it, you must add it in one of your databases.
CREATE EXTENSION IF NOT EXISTS dblink;
-- or
CREATE EXTENSION IF NOT EXISTS dblink WITH SCHEMA schema_name_here;
Using dblink function
dblink(conn_str, sql_query) you can execute dynamically generated SQL statements. The user you will use in the connection string matters, so, choose a user that can access all databases, schemas and tables involved.
As an example, the following SQL queries all databases for the table names from schemata table, in information_schema schema, filtering by columns named data_type.
select datname,
schema_name,
unnest(table_names) as table_name
from (select datname,
schema_name,
(select table_names
from dblink(
'dbname=' || datname || ' user=postgres password=postgres',
'select array_agg(table_name) as table_names from ' || schema_name || '.columns where column_name = ''data_type''')
as p(table_names character varying array)) as table_names
from (select datname,
unnest(schema_name_arr) as schema_name
from (select datname,
(select schema_name_arr
from dblink(
'dbname=' || datname || ' user=postgres password=postgres',
'select array_agg(distinct nspname) as schema_name_arr from pg_catalog.pg_namespace where nspname like ''information_schema''')
as t(schema_name_arr character varying array)) as schema_name_arr
from pg_catalog.pg_database
where datistemplate is false) q
where schema_name_arr is not null
) r
) s;
The main query here is this: select array_agg(table_name) as table_names from ' || schema_name || '.columns where column_name = ''data_type''.
Since dblink is being used in the SELECT clause, it is restricted to return only one column. That's why I'm using the combo ARRAY_AGG + UNNEST.
If you added dblink module into a schema, remember to use schema_name.dblink when calling that function.
Hope it helps. Happy coding! :)

Truncating table before dynamic SQL with looping variables inside of one function

I have a function that loops through specific schema names and inserts data into a table. I would like to be able to truncate said table before the insert loop occurs. I've tried putting the truncate statement inside of the dynamic query and that caused it to only keep schema's data inside of the table. I also tried declaring it as it's own variable and then executing the statement separately from the looping statement -- but that resulted in the same.
So my question is -- Where exactly would I put a truncate table dwh.prod_table_notify statement within this function? So that every time I run this function the table would be truncated and then the insert would properly loop through each schema being returned from the FOR statement.
NOTE: I'm forced to use postgres 8.2
CREATE OR REPLACE FUNCTION dwh.dim_table_notification()
RETURNS void
LANGUAGE plpgsql
AS $function$
Declare
myschema varchar;
sql2 text;
Begin
for myschema in
select distinct table_schema
from information_schema.tables
where table_name in ('dim_loan_type', 'dim_acct_type')
and table_schema NOT LIKE 'pg_%'
and table_schema NOT IN ('information_schema', 'ad_delivery', 'dwh', 'users', 'wand', 'ttd')
order by table_schema
loop
sql2 ='insert into dwh.prod_table_notify
select '''|| myschema ||''' as userid, loan_type_id as acct_type_id, loan_type::varchar(10) as acct_type, loan_type_desc::varchar(50) as acct_type_desc, term_code, 1 as loan_type from '|| myschema || '.' ||'dim_loan_type where term_code is null
union
select '''|| myschema ||''' as userid, acct_type_id, acct_type::varchar(10), acct_type_desc::varchar(50), term_code, 0 as loan_type from '|| myschema || '.' ||'dim_acct_type where term_code is null';
execute sql2;
end loop;
END;
$function$
CREATE OR REPLACE FUNCTION dwh.dim_table_notification()
RETURNS void LANGUAGE plpgsql AS
$func$
DECLARE
myschema text;
BEGIN
-- truncate simply goes here:
TRUNCATE dwh.prod_table_notify;
FOR myschema IN
SELECT quote_ident(table_schema)
FROM information_schema.tables
WHERE table_name IN ('dim_loan_type', 'dim_acct_type')
AND table_schema NOT LIKE 'pg_%'
AND table_schema NOT IN
('information_schema', 'ad_delivery', 'dwh', 'users', 'wand', 'ttd')
ORDER BY table_schema
LOOP
EXECUTE '
INSERT INTO dwh.prod_table_notify
(userid, acct_type_id, acct_type, acct_type_desc, loan_type)
SELECT '''|| myschema ||''', loan_type_id, loan_type::varchar(10)
, loan_type_desc::varchar(50), term_code, 1 AS loan_type
FROM '|| myschema || '.dim_loan_type
WHERE term_code IS NULL
UNION ALL
SELECT '''|| myschema ||''' AS userid, acct_type_id, acct_type::varchar(10)
, acct_type_desc::varchar(50), term_code, 0 AS loan_type
FROM '|| myschema || '.dim_acct_type
WHERE term_code IS NULL';
END LOOP;
END
$func$
Are you sure, you can actually use TRUNCATE? Quoting the manual for 8.2:
TRUNCATE cannot be used on a table that has foreign-key references from other tables, unless all such tables are also truncated in the same command.
If tables are small, DELETE is faster than TRUNCATE to begin with:
DELETE FROM dwh.prod_table_notify;
You have to sanitize identifiers! Use quote_ident(), also available in pg 8.2.
No point in using DISTINCT here.
Provide a column definition list for your INSERT. Else it can break in confusing ways, when you change the table later.
If rows in the two legs of the SELECT are unique, use UNION ALL instead of UNION. No point in trying to fold duplicates.

Is there a way to alter many tables to add default values to a common column name?

We have a set of 45 tables which carry a common column {variety}.
The need is to set all such columns with a default value {comedy}.
The ALTER TABLE (SCHEMA.TABLE_NAME) MODIFY(VARIETY DEFAULT 'COMEDY')
Will get it done, but I am wondering if there is a way to create a sql script in Oracle 11g that will change all tables within the schema which have a common coloumn name to the common default value.
DECLARE
cnt NUMBER;
BEGIN
FOR x IN (
SELECT DISTINCT t.table_name
FROM user_tables t
INNER JOIN user_tab_columns c ON c.table_name = t.table_name
) LOOP
EXECUTE IMMEDIATE 'ALTER TABLE (SCHEMA.' || x.table_name || ') MODIFY(VARIETY DEFAULT ''COMEDY'')';
END LOOP;
END;
The alter table statement can be written as following,
using alternate quoting mechanism.
'alter table ' || x.table_name || q'[ modify (variety default 'COMEDY')]'