use strict;
use warnings;
use DBI;
my $dbname = 'testdb'; # The testdb database must exist
my $server = 'localhost';
my $user = 'username';
my $pass = 'password';
my $dbh = DBI->connect(
"DBI:InterBase:" . "dbname=" . $dbname . ";host=" . $server .
";ib_dialect=3",
$user, $pass,
{ AutoCommit => 1, RaiseError => 1, FetchHashKeyName => 'NAME_lc' }
);
my $sql1 = qq{
CREATE TABLE testtable (
cant DECIMAL(9,3) NOT NULL,
vat DECIMAL(4,2) NOT NULL,
price NUMERIC(11,5) NOT NULL)
};
my $rv = $dbh->do( $sql1 );
my $sql2 = qq{
CREATE VIEW v_testtable (
cant
, vat
, price
, val
, vat_val
) AS SELECT
tt.cant
, tt.vat
, tt.price
, tt.cant * tt.price
, tt.cant * tt.price * tt.vat /100
FROM testtable tt;
};
$rv = $dbh->do( $sql2 );
# Insert some data
my $sql3 = qq{
INSERT INTO testtable (cant, vat, price)
VALUES (?, ?, ?);
};
my $st = $dbh->prepare($sql3);
my @testval = (
[ 1, 19, 100 ],
[ 1, 19, 1000 ],
[ 1, 19, 10000 ],
[ 1, 19, 100000 ],
);
foreach my $val ( @testval ) {
$st->execute( @{$val} );
}
$dbh->disconnect();
####
use strict;
use warnings;
use DBI;
my $dbname = 'testdb';
my $server = 'localhost';
my $user = 'username';
my $pass = 'password';
my $dbh = DBI->connect(
"DBI:InterBase:" . "dbname=" . $dbname . ";host=" . $server .
";ib_dialect=3",
$user, $pass,
{ AutoCommit => 1, RaiseError => 1, FetchHashKeyName => 'NAME_lc' }
);
my $sql4 = qq{
SELECT
cant
, vat
, price
, val
, vat_val
FROM v_testtable
};
eval {
my $st = $dbh->prepare($sql4);
$st->execute;
while ( my $rez = $st->fetchrow_hashref() ) {
print $rez->{cant} * $rez->{price} * $rez->{vat} /100,"\t";
print "$rez->{vat_val}\n";
}
};
if ($@) {
warn "Transaction aborted because $@";
}
$dbh->disconnect();
####
Expected Result
--------- -----------------
19 -88.1021438976
190 -884.1624455168
1900 -8847.1212166144
19000 -88475.1384243200
####
19 134.1051235328
190 1347.0641895424
1900 13474.0778692608
19000 134745.0736599040