in reply to Interpolating subroutine call in SQL INSERT INTO SELECT statement
Which database ?. This is for MySQL. Note user must have the correct permissions to create functions. The function is simply a demo, obviously you don't need a function to concat something to a field.
poj#!perl use strict; use warnings; use DBI; use Data::Dump 'pp'; my $dbh = get_dbh(); my $proc = " CREATE FUNCTION my_subrt(s CHAR(30)) RETURNS CHAR(50) RETURN CONCAT(s,'####'); "; $dbh->do('DROP FUNCTION IF EXISTS my_subrt'); $dbh->do($proc); #$dbh->do('DELETE FROM TABLE2'); my $sql = ' INSERT INTO TABLE2 (COLUMN1, COLUMN2, COLUMN3) SELECT COLUMN1, COLUMN2, my_subrt(COLUMN3) FROM TABLE1 WHERE COLUMN4=?'; my $rv = $dbh->do($sql, {}, 'foo'); my $ar = $dbh->selectall_arrayref('SELECT * FROM TABLE2'); pp $ar; $dbh->do('DROP FUNCTION my_subrt'); sub get_dbh{ my $database = "test"; my $user = ""; my $pw = ""; my $dsn = "dbi:mysql:$database:localhost:3306"; my $dbh = DBI->connect($dsn, $user, $pw, { RaiseError=>1, AutoCommit=>1 } ); return $dbh; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Interpolating subroutine call in SQL INSERT INTO SELECT statement
by shadowsong (Pilgrim) on Aug 26, 2015 at 14:42 UTC | |
by poj (Abbot) on Aug 26, 2015 at 19:57 UTC | |
by shadowsong (Pilgrim) on Aug 27, 2015 at 09:02 UTC |