CSharma has asked for the wisdom of the Perl Monks concerning the following question:
Hi Everyone! I'm working on a script to copy/update data from mysql table to oracle table. Requirements are: i.) if record exists in oracle, then update it (if data is old) ii.) if record doesn't exist, simple insert it into oracle I'm facing issue in insertion and update into oracle. My compatible oracle table to mysql is as below:
CREATE TABLE cloverleaf_ingestion( pk NUMBER(20), File_name VARCHAR2(200), Cl_file_size_in_bytes NUMBER(22), Cl_checksum VARCHAR2(32), CL_created_date_time DATE, Status VARCHAR2(20), Mn_file_size_in_bytes NUMBER(22), Mn_checksum VARCHAR2(32), Failed_retries NUMBER(11), Processed_date_time DATE, ftp_acknowledgement VARCHAR2(20), ohf_processing_status VARCHAR2(20), ohf_processing_datetime DATE, ohf_total_rows NUMBER(22), ohf_failed_rows NUMBER(22), ohf_processed_rows NUMBER(22), hdfs_processing_status VARCHAR2(20), hdfs_processing_datetime DATE, hdfs_checksum VARCHAR2(60), hdfs_file_size_in_bytes NUMBER(22), CONSTRAINT cloverleaf_ingestion_pk PRIMARY KEY (pk) );
What I've done is: i.) below hash to check if data exists in oracle. ii.) checks hash and then inserts or updates the record in oracle (for now done only insert).
sub getOracleRowHahs{ my $odbh = DBI->connect($oracledsn, $oracleuser, $oraclepass) || d +ie "Can't connect to oracle db:$DBI::errstr\n"; my $osth = $odbh->prepare("select * from emp"); $osth->execute(); my $tableref; while(my $row = $osth->fetchrow_hashref()){ no warnings; $tableref->{$row->{'PK'}} = $row; } $odbh->disconnect(); return $tableref; }
my $sdbh = DBI->connect($sqldsn, $sqluser, $sqlpass) || die "Can't + connect to mysql db:$DBI::errstr\n"; my $odbh = DBI->connect($oracledsn, $oracleuser, $oraclepass) || d +ie "Can't connect to oracle db:$DBI::errstr\n"; my $oracleTableHash = &getOracleRowHahs(); no warnings; my $query = "select * from $table"; my $ssth = $sdbh->prepare($query) || die "Error in prepare t +o mysql: $DBI::errstr\n";; $ssth->execute() || die "Error in execute to mysql: $DBI::er +rstr\n";; while(my @row = $ssth->fetchrow_array()){ # say join("\t",@row); if(!(exists($oracleTableHash->{$row[0]}))){ my $columns = join(",",@row); say $columns; my $insertquery = "insert into $table values(?,?,?,?,?,?,? +,?,?,?,?,?,?,?,?,?,?,?,?,?)"; #say $insertquery; #exit(); my $osth = $odbh->prepare($insertquery) || die "Error in p +repare: $DBI::errstr"; $osth->execute(@row); } } $sdbh->disconnect() || die "Error in disconnect to mysql: $D +BI::errstr\n"; $odbh->disconnect() || die "Error in disconnect to oracle: $ +DBI::errstr\n";
could someone please help me out?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: copying mysql table data to oracle table
by huck (Prior) on Aug 24, 2017 at 10:45 UTC | |
by CSharma (Sexton) on Aug 24, 2017 at 12:41 UTC | |
by huck (Prior) on Aug 24, 2017 at 13:11 UTC | |
by CSharma (Sexton) on Aug 25, 2017 at 07:52 UTC | |
Re: copying mysql table data to oracle table
by thanos1983 (Parson) on Aug 24, 2017 at 09:42 UTC | |
by soonix (Chancellor) on Aug 24, 2017 at 11:35 UTC | |
by thanos1983 (Parson) on Aug 24, 2017 at 13:23 UTC | |
Re: copying mysql table data to oracle table
by chacham (Prior) on Aug 24, 2017 at 11:54 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |