in reply to using sequences with dbi
use strict; use dbi; use dbd::oracle; #-- Define constants use constant TRUE => 1; use constant FALSE => 0; use constant ORA_USER => 'myschema'; use constant ORA_PASS => 'sesame'; use constant ORA_TNS => 'tst00'; use constant ERR_OK => 0; #-- Define variables my $mDBHandle; my $mSQLInsertHandle; my $mSQLSelectHandle; my $mSQLInsert = <<'EOSQL'; INSERT INTO test (val, name) VALUES (test_seq.nextval,?) EOSQL my $mSQLSelect = <<'EOSQL'; SELECT val, name FROM test EOSQL my @mTuple; #-- Connect to the database $mDBHandle = DBI->connect ( 'dbi:Oracle:' . ORA_TNS, ORA_USER, ORA_PASS, { AutoCommit => FALSE, PrintError => FALSE, RaiseError => FALSE, } ) || die $DBI::errstr.' - '.$DBI::err; #-- Prepare the SQL statements $mSQLInsertHandle = $mDBHandle->prepare($mSQLInsert) || die $DBI::errs +tr.' - '.$DBI::err; $mSQLSelectHandle = $mDBHandle->prepare($mSQLSelect) || die $DBI::errs +tr.' - '.$DBI::err; #-- Display records $mSQLSelectHandle->execute() || die $DBI::errstr.' - '.$DBI::err; print "Val\tName\n"; print "---\t----\n"; while (@mTuple = $mSQLSelectHandle->fetchrow_array) { print $mTuple[0],"\t",$mTuple[1],"\n"; } print "\n\n"; #-- Execute the SQL insert statements $mSQLInsertHandle->execute('test1') || die $DBI::errstr.' - '.$DBI::er +r; $mSQLInsertHandle->execute('test2') || die $DBI::errstr.' - '.$DBI::er +r; #-- Commit the inserts $mDBHandle->commit(); #-- Display records $mSQLSelectHandle->execute() || die $DBI::errstr.' - '.$DBI::err; print "Val\tName\n"; print "---\t----\n"; while (@mTuple = $mSQLSelectHandle->fetchrow_array) { print $mTuple[0],"\t",$mTuple[1],"\n"; } print "\n\n"; #-- Disconnect from the database $mDBHandle->disconnect(); #-- Exit exit(ERR_OK); #-- End of Script
|
|---|