Skip to content

Instantly share code, notes, and snippets.

@theSuiGenerisAakash
Last active February 12, 2020 03:44
Show Gist options
  • Save theSuiGenerisAakash/f41a325d01e75e5fe2826043eadf6f85 to your computer and use it in GitHub Desktop.
Save theSuiGenerisAakash/f41a325d01e75e5fe2826043eadf6f85 to your computer and use it in GitHub Desktop.
Perform any DML/DDL operations on selective tables, selective columns and a mix of those
-- A simple script that allows you to perform update and delete operations with optional where clause but mainly giving
-- you the flexibilty of doing so across multiple db, tables and columns
do $$
declare
selectrow record;
begin
for selectrow in
select
'DELETE FROM '|| t.mytable || ' WHERE ' || t.mytable ||'.sampledate > ''12-31-2018''' as script
from
(
select tablename as mytable from pg_tables where schemaname = 'public' and tablename in (SELECT table_name
FROM information_schema.columns
WHERE table_schema='public' AND column_name='sampledate')
) t
loop
execute selectrow.script;
end loop;
end $$
@theSuiGenerisAakash
Copy link
Author

This is a very specific example, my bad!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment