# With placeholders my $sth_test = $dbh->prepare("select distinct cast($date_col as date) from ?;") or die; $sth_test->execute($schema.$table); # Errors (on execute()) INFO: The database has been opened successfully. DBD::Pg::st execute failed: ERROR: syntax error at or near "$1" LINE 1: select distinct cast(date as date) from $1; ^ at ./refresh_dates.pl line n. DBD::Pg::st execute failed: ERROR: syntax error at or near "$1" LINE 1: select distinct cast(date as date) from $1; ^ at ./refresh_dates.pl line n. # Without placeholders my $sth_test = $dbh->prepare("select distinct cast($date_col as date) from $schema.$table;") or die; $sth_test->execute(); $dbh->disconnect(); # Error (on disconnect) DBI::db=HASH(0x32cec98)->disconnect invalidates 1 active statement handle (either destroy statement handles or call finish on them before disconnecting) at ./refresh_dates.pl line n.