sql - Update multiple columns that start with a specific string -
i trying update bunch of columns in db testing purposes of feature. have table built hibernate of columns created embedded entity begin same name. i.e. contact_info_address_street1
, contact_info_address_street2
, etc.
i trying figure out if there way affect of:
update table set contact_info_address_* = null;
if not, know can long way, looking way myself out in future if need on again different set of columns.
there's no handy shortcut sorry. if have kind of thing lot, create function dynamically execute sql , achieve goal.
create or replace function reset_cols() returns boolean $$ begin execute (select 'update table set ' || array_to_string(array( select column_name::text information_schema.columns table_name = 'table' , column_name::text 'contact_info_address_%' ),' = null,') || ' = null'); return true; end; $$ language plpgsql; -- run function select reset_cols();
it's not nice though. better function 1 accepts tablename , column prefix args. i'll leave exercise readers :)
Comments
Post a Comment