Last active
February 12, 2020 03:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 $$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a very specific example, my bad!