I have several schemas in my database. Most of the schemas have a table called orders. How do I iterate through the schemas that have that table and get a count of the number of records in each of those schemas for that table?
select * from information_schema.schemata;
see https://www.postgresql.org/docs/11/infoschema-schemata.html
do
$$
declare
tableName varchar := 'xyz';
schemaName varchar;
query text;
counter integer;
begin
for schemaName in select table_schema from information_schema.tables where table_name = tableName
loop
query := 'select count(*) from ' || schemaName || '.' || tableName;
execute query into counter;
raise notice E'% %', schemaName, counter;
end loop ;
end $$
Related
Please what is the effective way for getting the rows where the first column (primary index) is populated but all other columns are null? The table has 25 columns and I want to avoid putting all column names in the WHERE clause.
Thanks.
One of many methods (this will give you a generated SQL to run, but you can amend the code just to get its results through insert select, for instance)
create table test_table_2000 ( charcol1 varchar(2000) );
replace procedure sp_find_blank_rows( in_database varchar(50), in_tablename varchar(50) )
begin
declare
l_sql varchar(2000);
declare
l_int integer;
set l_sql = 'select * from '||in_database||'.'||in_tablename||' where 1=1 ';
FOR fReq AS cReq CURSOR FOR
select * from Dbc.Columns where databaseName=in_database and TableName=in_tablename order by ColumnId
DO
IF l_int is null THEN
SET l_sql = l_sql || ' and ' || fReq.ColumnName || ' is not null ';
ELSE
SET l_sql = l_sql || ' and ' || fReq.ColumnName || ' is null ';
END IF;
set l_int = 1;
END FOR;
insert into test_table_2000 values ( l_sql );
end;
call sp_find_blank_rows('<your_database>','<your_table>');
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;
select *
from (
select table_name
from user_tab_cols
where column_name='CONTRACT_NUMBER'
)
where contarct_number ='U5490231'
in this select statement, I am trying to get the list of tables from user_tab_cols
and then do a
select *
from table_name
where column_name='U5490231'.
trying to find a select statement on this basis but getting error
Oracle Query:
v_table NVARCHAR(MAX)
v_whereCol NVARCHAR(MAX)
v_WhereParam NVARCHAR(MAX)
v_WhereParam := 'CONTRACT_NUMBER'
v_whereCol := 'U5490231'
SELECT E.table_name INTO v_table
FROM user_tab_cols AS E
WHERE E.column_name = v_WhereParam
v_query NVARCHAR(MAX)
v_query := 'Select * from ' || v_table || ' Where contarct_number =''' || v_whereCol || ''''
EXECUTE IMMEDIATE v_query
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?