smith19 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I am new to perl and i need help in getting output of a sql statements to one variable .can anyone provide me a sample perl code.Below are the sql's i am running and i kept all those sql in one file select instance_name,host_name from v\$instance; select sysdate from dual; select status,destination,dest_id from v\$archive_dest where status ='VALID';

below is the sample code i wrote
my $dbh = DBI->connect ("dbi:Oracle:$v_primary_db","username","passwor +d",{ RaiseError => 1, AutoCommit => 0, ora_session_mode => ORA_SYSDBA + }) || die "Database connection not made: $DBI::errstr"; open (SQL, "$primarysql"); while (my $sqlstatement = <SQL>) { $sth = dbi->prepare($sqlstatement); $sth ->execute(); while (my @row = $sth->fetchrow_array() ){ foreach (@row) { $_="\t" if !defined($_); print "$_\t"; } print "\n"; } } END { $dbh->disconnect if defined($dbh); } }

Replies are listed 'Best First'.
Re: multiple sql command output to a array variable
by kcott (Archbishop) on Oct 18, 2013 at 06:53 UTC

    G'day smith19,

    Welcome to the monastery.

    Where you have "dbi->prepare", you probably want "$dbh->prepare".

    Prior to that line, you may want "chomp $sqlstatement;". You could add a "print $sqlstatement;" to check the string you're passing to prepare().

    -- Ken

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: multiple sql command output to a array variable
by Anonymous Monk on Oct 18, 2013 at 06:47 UTC

    What is the problem, do you have some error messages to share?

    I am not familiar with v\$instance what is that?

    Have you seen DBI recipes ?