Search code examples
perloracle-sqldeveloper

Use special character in Perl Script


I try to execute a script to get data from a database with a Perl Script, but my query contain a special character because I get the information form v$process in Oracle.

I use these lines:

   my $dbh = DBI->connect ( "dbi:Oracle:$TNS", $USER, $PASS ) || die ($DBI::errstr . "\n");
    $dbh->do("ALTER SESSION SET NLS_DATE_FORMAT = 'yyyy-mm-dd hh24:mi:ss'");
    $dbh->{AutoCommit} = 0;
    $dbh->{RaiseError} = 1;
    $dbh->{ora_check_sql} = 0;
    $dbh->{RowCacheSize} = 32;
    #Sessions, Datafiles
    my $wstmt = qq{ 
    select 'PROCESOS',count(*) AS 'TOTALPROCESOS' from v$process
    };
    my $sth = $dbh->prepare( $wstmt );
    $sth->execute();
    my $ref;
    while($ref = $sth->fetchrow_arrayref) {
    print join (",", @{$ref}), "\n";
    }
    $sth->finish();
    $dbh->disconnect if defined( $dbh ); 

But the result is

ORA-04044: procedure, function, package, or type is not allowed here (DBD ERROR: error possibly near <*> indicator at char 57 in '
    select 'PROCESOS',count(*) AS "TOTALPROCESOS" from <*>v
    ') [for Statement "
    select 'PROCESOS',count(*) AS "TOTALPROCESOS" from v

Solution

  • Double quote operators, like qq, interpolate variables. $process looks like a variable in Perl. If you want the literal string $process, use a single quote operator like q:

    my $wstmt = q{ 
    select 'PROCESOS',count(*) AS 'TOTALPROCESOS' from v$process
    };