Posts

Showing posts from August, 2023

DB2: To get create time and alter time for procedures

 select * from syscat.routines where routinename = 'TMW_UPDATE_AVAILABILITYDATE' with ur;

Postgres: Find all the foreign keys

SELECT conrelid::regclass AS table_from , conname , pg_get_constraintdef(oid) FROM pg_constraint WHERE contype IN ( 'f' , 'p ' ) AND connamespace = 'public' ::regnamespace -- your schema here ORDER BY conrelid::regclass::text, contype DESC ; SELECT c.conname AS constraint_name, c.contype AS constraint_type, sch.nspname AS "self_schema", tbl.relname AS "self_table", ARRAY_AGG(col.attname ORDER BY u.attposition) AS "self_columns", f_sch.nspname AS "foreign_schema", f_tbl.relname AS "foreign_table", ARRAY_AGG(f_col.attname ORDER BY f_u.attposition) AS "foreign_columns", pg_get_constraintdef(c.oid) AS definition FROM pg_constraint c LE...

Postgres: drop foreign key

 alter table ordadmin.note_association drop constraint note_association_order_header_fk;

Postgres: row count for all tables in that schema

 select table_schema, table_name,        (xpath('/row/count/text()', query_to_xml('select count(*) from '||format('%I.%I', table_schema, table_name), true, true, '')))[1]::text::int as row_count from information_schema.tables where table_schema = 'tailoring' order by table_name asc or  with tbl as     (SELECT table_schema,table_name      FROM information_schema.tables        where table_name not like 'pg_%' and table_schema in ('cfadmin','ctlgadmin','ctlgadmin2','cmsadmin','cmtadmin','gcpcustomjobs','grpadmin','invadmin','ordadmin','fivertranuser','cfaudit'))    select table_schema, table_name,         (xpath('/row/c/text()', query_to_xml(format('select count(*) as c from %I.%I', table_schema, table_name), false, true, '')))[1]::text::int as rows_n  from tbl  ORDER BY 3 DESC;

Oracle: To spool to CSV

  set   colsep ,   -- separate columns with a comma set  pagesize 0  -- No header rows set  trimspool  on  -- remove trailing blanks set  headsep  off   -- this may or may not be useful...depends on your headings. set  linesize X  -- X should be the sum of the column widths set  numw X  -- X should be the length you want for numbers (avoid scientific notation on IDs) spool myfile.csv select  table_name, tablespace_name from  all_tables