
steve798
Novice
Nov 10, 2017, 11:17 AM
Post #1 of 1
(2510 views)
|
Script to read a 0racle table and output to a csv format file
|
Can't Post
|
|
I am using Perl version 5.8.8 on Windows. The perl script is to read from a oracle 11g database table and output a file in csv format.
# This script connects to an oracle database, reads columns from region based on a selection criteria and output records in a csv # use strict; use DBI; use warnings; use File::Copy; use Text::CSV; my $dbh = DBI->connect("dbi:Oracle:$db",$user,$pw) or die "Database connection not made: $DBI::errstr"; { my $sth = $dbh->prepare("SELECT * FROM region"); $sth->execute; my $names = $sth->{'NAME'}; my $tbl_data = $sth->fetchall_arrayref; print join "\n", map { join(',', map { "\"$_\""' } @$_ } @$tbl_data; $dbh->disconnect(); } I need help updating the Perl script that I have thus far to: a. restrict the select statement with a where clause (below code) and use the below where clause condition as an input to the script. b. update statement (as below) once the file is written to a csv.
select region_date, cntr_name from region where cntr_name in ('US' , 'CAN') and flag = 'Y' and part_key = (select ('S_' || v.tsched || '_' || v.cgroup) as partition_key from rgvars v WHERE v.regdate = -- "input parameter" and v.cgroup = -- "input parameter" and v.dnumber = -- "input parameter "); -- update region set flag = 'N'; -- update the table after the file is written out in a csv format
(This post was edited by steve798 on Nov 10, 2017, 11:20 AM)
|