Does Perl DBI Support Oracle Subquery Factoring? - oracle

I have searched online for several days and cannot find an answer.
Does Perl DBI support Oracle Subquery Factoring (i.e. WITH-clause)?
As an example, the simple Perl DBI application further below fails with the error:
DBD::Oracle::st fetchrow_array failed: ERROR no statement executing (perhaps you need to call execute first)
Simple Perl DBI Application:
#!/bin/perl
use DBI;
my $sql = <<END_SQL;
WITH w AS
(
SELECT wafer_seq
FROM wafer
WHERE load_time > sysdate - 1
)
SELECT v.*
FROM vwafer v, w
WHERE v.wafer_seq = w.wafer_seq
ORDER BY v.wafer_seq
END_SQL
my $dbh = DBI->connect('DBI:Oracle:<schema_id>', '<username>', '<password>');
my $sth = $dbh->prepare($sql) || die "ERROR PREP";
$sth->execute() || die "ERROR EXEC";
while (my #row = $sth->fetchrow_array())
{
print "#row\n";
}
$sth->finish();
$dbh->disconnect();
exit 0;
This same application will work if I simply change the SQL to:
SELECT v.*
FROM vwafer v,
(
SELECT wafer_seq
FROM wafer
WHERE load_time > sysdate - 1
) w
WHERE v.wafer_seq = w.wafer_seq
ORDER BY v.wafer_seq
Finally, I confirmed that both SQLs mentioned above work when executed directly in a database visualizer application (e.g. DBVisualizer).

It appears my version of Perl (5.8.8), DBI (1.58), and/or DBD::Oracle (1.19) do not support Oracle Subquery Factoring.
I was able to successfully execute the same Perl application through updated Perl (5.12.1), DBI (1.613), and DBD::Oracle (1.24) versions.
Unfortunately, even after reading change histories for Perl, DBI, and DBD::Oracle, I do not know exactly which component introduced support of Oracle Subquery Factoring. My suspicion is the DBI Oracle driver (DBD::Oracle).

Perl DBI correctly processed 'WITH' clause.
I just now check it and test your code
According to error there is may by only one case You forget to call
$sth->execute();
Double check please are it called, may be it was commented or something else.
But your code work correct.

Related

Oracle sqlplus execute sql script from command line while passing parameters to the sql script

I have one script script.sql which I want to execute from command line using oracle and passing it two parameters, as shown below
sqlplus user/pass # script.sql my_parameter1_value my_parameter2_value
What should it be in script.sql in order to be able to run it with the parameter values?
The solution can be prepared looking at oracle blogs:
https://blogs.oracle.com/opal/sqlplus-101-substitution-variables#2_7
For the question above, the solution would be to create a script.sql like this:
DEFINE START_VALUE = &1;
DEFINE STOP_VALUE = &2;
SELECT * FROM my_table
WHERE
value BETWEN &&START_VALUE AND &&STOP_VALUE;
I wanted to run a script that would return all orders raised during the last seven days. Here's how...
the script
SELECT * FROM orders_detail WHERE order_date BETWEEN '&1' AND '&2';
EXIT;
the command
sqlplus ot/Orcl1234#xepdb1 #"/opt/oracle/oradata/Custom Scripts/orders_between_dates.sql" $(date +%d-%b-%Y -d '-7 days') $(date +%d-%b-%Y)
Hope that helps someone. Luck.

Quoting error when executing a Drill query from JetBrains JDBC driver

I have a custom datasource driver in JetBrans (Rider 2019.2) which uses apache-drill-1.17.jar JDBC driver (official).
Using the driver results in this error:
SELECT * FROM dfs.my_parquets."Test" limit 10;
--
PARSE ERROR: Lexical error at line 1, column 19. Encountered: "`" (96), after : ""
SQL Query: ALTER SESSION SET `exec.query.max_rows`=501
From the error is obvious that Rider tries to execute this hidden query with backticked identifiers:
ALTER SESSION SET `exec.query.max_rows`=501
The problem is that the quoting_identifiers in target drill are not set to ` (backtick) but to " (double quote).
As a connection string I'm using this: jdbc:drill:drillbit=my-drill-instance;quoting_identifiers='"'
Is there a way to tell the driver to use double quotes in the hidden queries?
Manual shows, that option should be passed without quotes:
jdbc:drill:zk=local;quoting_identifiers=[
jdbc:drill:drillbit=my-drill-instance;quoting_identifiers="

How to do BULK INSERT in Oracle Database

I am trying to do a bulk insert into tables from a CSV file using Oracle11. My problem is that the database is on a remote machine which I can sqlpl to using this:
sqlpl username#oracle.machineName
Unfortunately the sqlldr has trouble connecting using the following command:
sqlldr userid=userName/PW#machinename control=BULK_LOAD_CSV_DATA.ctl log=sqlldr.log
Error is:
Message 2100 not found; No message file for product=RDBMS, facility=ULMessage 2100 not found; No message file for product=RDBMS, facility=UL
Now having given up on this approach I tried writing a basic sql script, but I am unsure of the proper Oracle keyword for BULK. I know this works in MySql but I get:
unknown command beginning "BULK INSER..."
When running the script:
BULK INSERT <TABLE_NAME>
FROM 'CSVFILE.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
I don't care which one works! Either one will do, I just need a little help.
Sorry I am a dumb dumb! I forgot to add oracle/bin to my path!
If you have found this post, add the bin directory to your path (linux) using the following commands:
export ORACLE_HOME=/path/to/oracle/client
export PATH=$PATH:$ORACLE_HOME/bin
Sorry if I wasted anyone's time ....

Fetching SQL execution plan. PL/SQL Developer = strange behaviour. SQL*Plus = no rows selected

I have a problem with fetching SQL plans.
In the final form, I have to fetch them through PL*SQL.
So I have a bash script, a loop to iterate through sql plan names and parameters, and a code like this:
sqlplus -s /nolog > /dev/null 2>&1 <<EOF
CONNECT BLAH/BLAH#BLAH
*CLEAR AND SET COMMANDS HERE*
VARIABLE RC REFCURSOR;
SPOOL ${BERICHT}_${configArray[0]}.DATA
SET TIMING ON;
EXEC :RC := $aktuellesBericht;
SET TIMING OFF;
PRINT RC;
DISCONNECT
QUIT
EOF
And the second part: (logging in as SYS, but without the SYSDBA permissions, I dont have them and I dont think that I will have them...)
sqlplus -s /nolog > /dev/null 2>&1 <<EOF
CONNECT SYSBLAH/SYSBLAH#BLAH
SPOOL ${BERICHT}_${configArray[0]}.SQLPLAN.TXT
CLEAR BREAK
CLEAR COMP
CLEAR COL
select
sqlplan.operation,
sqlplan.options,
sqlplan.object_name,
sqlplan.cost,
sqlplan.depth
from v\$sqlarea sqlarea,
v\$session sesion,
v\$sql_plan sqlplan
where sesion.sql_hash_value = sqlarea.hash_value
and sesion.sql_address = sqlarea.address
and sqlarea.plan_hash_value = sqlplan.plan_hash_value
and sesion.username = 'BLAH' order by sqlplan.depth;
QUIT
EOF
All I can get from this is *.SQLPLAN.TXT files containing just one sentence: no rows selected
What is strange here, that when I do the same in PL/SQL Developer - i get the same results, BUT when I just click on the Auto Refresh timer button on both SQL windows, both queries are running in parallel, and SOMETIMES the second query (the one to fetch SQL plan) is giving me results. And sometimes it doesnt.
It seems that theese commands need to be run in parallel... or am I missing something?
The sql query in my question was never needed.
All I needed is to write to the admin team to grant me the SELECT premissions on:
V$SQL_PLAN, V$SESSION and V$SQL_PLAN_STATISTICS_ALL
The answer to my question was found here.
I will need a deeper understanding of the problem in the future but for now, FWIK, looking deeper into the dbms_xplan package gave me the idea.
The approach to get the queries from the SYSACC account was the wrong idea. This acc has access to all the queries history and it is hard and painful (if even possible) to husk out the sql plan I needed.

MySql queries from elisp?

How can I make SQL queries from within emacs scripts to MySql then print the result set?
#!/usr/bin/emacs --script
(setq sql "select id, name
from foobar
order by name
")
(princ sql)
If you need a really reliable way to use mysql I would suggest using python mysql libraries via pymacs.
If you need it just working use mysql comand line client? Mysql is quite easy to use in scripts.
Check out the example below
$ mysql -u root -p<password> -B <db> --execute="select * from projects"
p_id p_timestamp p_name p_manager_id p_status
1 2009-04-14 14:42:00 Test1 3 1
2 2009-04-14 14:42:13 Test2 3 1
The output should be easy to interpret as it looks like tab separated CSV. So it should be ok for most scenarios.
These may assist you:
http://www.emacswiki.org/emacs/SqlMode
http://atomized.org/2008/10/enhancing-emacs%E2%80%99-sql-mode/

Resources