Need help in printing a sql query in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in printing a sql query in perl
# 1  
Old 05-21-2009
Question Need help in printing a sql query in perl

Hi All,

I have the following sql query

select abcd from udbadm.log where xyz='1'.

I have 16k queries similar to this with different values for xyz.

I want to print the values of 'abcd' for each row.

I have the following perl code, but not sure how i can print that particular column

open(OUTPUT, ">/tmp/output.txt") || die ("Can't open\n");
open (FILE, "/tmp/commands.sql") || die ("Can't open commands.sql\n");
while (<FILE>)
{
$sql = $_;
chomp $sql;
$update = $dbh->do($sql);
if($update)
{
print OUTPUT "Success:Now executing $sql\n"; - Here i want to print 'abcd' instead of the query for each run
}
else
{
print OUTPUT "Failure:";
$update = null;
$sql=$_;
$update = $dbh->do($sql);
print OUTPUT "Now executing $sql\n";
}
}


Appreciate your help. Thanks in advance
# 2  
Old 05-21-2009
Quote:
Originally Posted by userscript
....
I want to print the values of 'abcd' for each row.

I have the following perl code, but not sure how i can print that particular column
...
Here's one way to do it, using the "EMP" demonstration table in the standard "scott" schema of Oracle.

Code:
$
$ # display the contents of commands.sql
$ cat commands.sql
select deptno from emp where empno = 7369
select deptno from emp where empno = 7499
select deptno from emp where empno = 7521
select deptno from emp where empno = 7566
select deptno from emp where empno = 7654
select deptno from emp where empno = 7698
select deptno from emp where empno = 7782
select deptno from emp where empno = 7788
$
$ # display the contents of test_dbi.pl perl program
$ cat test_dbi.pl
#!perl -w
use DBI;
$dbh = DBI->connect('DBI:Oracle:','scott','tiger',
                    {RaiseError => 1, AutoCommit => 1});
open(OUTPUT, ">output.txt") or die "Can't open output.txt: $!";
open (FILE, "<commands.sql") or die "Can't open commands.sql: $!";
while (<FILE>) {
  chomp($sql = $_);
  $dbh->do($sql);
  $sth = $dbh->prepare($sql);
  $sth->execute();
  while (@row = $sth->fetchrow_array) {
    print OUTPUT $row[0],"\n";
  }
  $sth->finish();
}
$dbh->disconnect();
close(FILE) or die "Can't close commands.sql: $!";
close(OUTPUT) or die "Can't close output.txt: $!";
$
$ # run the perl program
$ perl test_dbi.pl
$
$ cat output.txt
20
30
30
20
30
30
10
20
$
$

Hope that helps,
tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Getting error in sql query

Hi All , I have tried many times am getting syntax error on 'UNION' can anybody tell me ... INSERT INTO table1 ( Type , num_items , num_letters , total_value ) (select type='1', num_items, num_letters=count(*), total_value=sum(letter_value) from table2 where num_items = 1 (1 Reply)
Discussion started by: Venkatesh1
1 Replies

2. Shell Programming and Scripting

Run SQL thru shell script: how to get a new line when run sql query?

Hi, this's Pom. I'm quite a new one for shell script but I have to do sql on shell script to query some information from database. I found a concern to get a new line...When I run my script, it retrieves all data as wondering but it's shown in one line :( What should I do? I'm not sure that... (2 Replies)
Discussion started by: Kapom
2 Replies

3. Shell Programming and Scripting

problem in SQL query

I used the following code code select * from tablename where columnname Instead of printing the expected output it prints all the files in the present directory since there is a "*" in the code. Is there any way to overcome the problem? Thanks Ananth (2 Replies)
Discussion started by: Ananthdoss
2 Replies

4. Shell Programming and Scripting

perl- oracle sql query

Hi, I am new to perl.How to query oracle database with perl??? Thanks (1 Reply)
Discussion started by: tdev457
1 Replies

5. Shell Programming and Scripting

run sql query via perl script

Hello, If I run this command on the server it works. # dbc "update config set radio_enabled = 0;" how can I execute the same command in perl. I have defined the dbc path. Can any one please correct the last line. #!/usr/bin/perl #database path $dbc='/opt/bin/psql -Userver... (0 Replies)
Discussion started by: sureshcisco
0 Replies

6. UNIX for Advanced & Expert Users

Passing Hash variable in to sql query in perl

Hi Everyone, Can anyone help me how do i call hash variable in to sql query in perl. Please see the script below i have defined two Hash %lc and %tab as below $lc{'REFF'}='V_RES_CLASS'; $lc{'CALE'}='V_CAP_CLASS'; $lc{'XRPD'}='V_XFMR_CLASS'; $tab{'V_RES_CLASS'}='V_MFR_SERS';... (6 Replies)
Discussion started by: jam_prasanna
6 Replies

7. Shell Programming and Scripting

How to use sql data file in unix csv file as input to an sql query from shell

Hi , I used the below script to get the sql data into csv file using unix scripting. I m getting the output into an output file but the output file is not displayed in a separe columns . #!/bin/ksh export FILE_PATH=/maav/home/xyz/abc/ rm $FILE_PATH/sample.csv sqlplus -s... (2 Replies)
Discussion started by: Nareshp
2 Replies

8. UNIX and Linux Applications

SQL Lite query

Hello Everyone, I am looking to write a script that will run on many machines in a network at the same time. They need to write a result to a common location. I plan to use a SQLlite database as this common writing point. But the concern I have is how SQLlite will react to multiple writes that... (1 Reply)
Discussion started by: garric
1 Replies

9. Shell Programming and Scripting

& in SQL query

I have a script that looks for all jobs that contain a particular calendar. Some of the calendars have '&' in them and sql freaks out when it encounters that.. is there a way around this? I have tried: select job_name from job where run_calendar='1&15dom' select job_name from job... (3 Replies)
Discussion started by: Lindarella
3 Replies

10. Shell Programming and Scripting

Incorrect SQL query in perl

I the the following perl script, however it does not return anything. Did i form my syantax wrongly? $sth=$dbh->prepare("select a.rowid,a.* from Table1 a where a.field1 = \'$Indicator1\' and a.schedule = \'$Indicator2\' and a.lastdate <= sysdate"); $sth->execute() ... (1 Reply)
Discussion started by: new2ss
1 Replies
Login or Register to Ask a Question