ORACLE ORA00907: Differences in connection string using OraOLEDB.Oracle vs TNS lookup via Oracle in OraClient10g_home3 - sql

I am using Oracle Client 10.2g and by changing my connection string to the Oracle database I now get error ORA00907 for some of my queries.
The code is executing within excel 2010 using VBA and I can run 20+ quires without error using the following connection string:
ServerConnectionString="Driver={Oracle in OraClient10g_home1};Dbq=DBNAME;Uid=USERNAME;Pwd=PASSWORD;"
With OpenR2DBConnection
.ConnectionTimeout = ConnectionTimeout
.Open ServerConnectionString
.Execute "ALTER SESSION SET NLS_DATE_FORMAT = 'DD/MM/YYYY'"
.CommandTimeout = CommandTimeout
End With
By changing the connection string only to:
ServerConnectionString="Provider=OraOLEDB.Oracle;Data Source=(<<<data exact translation from TNSNAMES file for the DBNAME>>>);User id=USERNAME;Password=PASSWORD;"
2 of the 20+ queries fail with ORA-00907: missing right parenthesis
One of the Queries that fails (refracted):
select n1.name,n1.sdate,n1.edate,n1.note as crnote,n1.cdate ,n1.pri
from
(
select n.name,n.sdate,n.edate,n.note,n.cdate,n.pri , RANK() OVER (PARTITION BY n.sdate,pri ORDER BY (n.adate - n.cdate) asc,n.pri asc) RANK
from
(
select mmpe.name,mmpe.sdate,mmpe.edate,mmpe.cannote as note,mmpe.cdate,mmip.crdate as adate,mp.pri
from mmpe ,mmmip mmip, mp
where <<clauses1>>
and mmpe.name in (<<list of strings>>)
and mmip.name(+) = mmpe.name
and <<more clauses2>>
union
Select mmip.name,mmip.sdate,greatest(<<formula>>) as edate , <<create note>> as note ,mmip.crdate as cdate, td.adate,mp.pri
From mi , mmip,td, mp
Where <<clauses3>>
and mi.name in (<<list of strings>>)
union all
Select mmip.name,mmip.sdate,mmip.edate as edate , <<create note>> as note ,mmip.crdate as cdate , td.adate,mp.pri
From mi , mmip,td, mp
Where <<clauses4>>
and mi.name in (<<list of strings>>)
union
Select mmip.name,mmip.sdate,td.cddate-1 as edate , <<create note>> as note, mmip.crdate as cdate,mmip.crdate as adate,mp.pri
From mi , mmip,td, mp
Where <<clauses4>>
and mi.name in (<<list of strings>>)
) n
) n1
where rank = 1
order by 6,2,5;
I have tested that the Query runs correctly in Oracle SQL Developer.
I have verified that prior to Executing the query the SQL statements are identical for both connection strings.
The other query that fails is also using a union and rank function but it is not the only one.
The reason I wish to use the OraOLEDB.Oracle connection is that I am attempting to remove my reliance on the tnsnames.ora files as from time to time I add new database instances and want to avoid all my users having to update this file in the oracle directory.
Lastly the ORACLE database is version 8.
Any help would be greatly appreciated,
Thanks in advance!
Updated: Removed typo error

OK I've work it out.
Thanks to all those that spent any time thinking about it.
When I refracted the example above I removed inline comments denoted by '--'.
It was these comments that caused the ORA-00907 error with the OraOLEDB.Oracle driver.
So the fix is simple: Remove comments from the SQL command!
Thanks again!

Related

Apache Drill Timestampdiff on Oracle DB

Hey everyone im relativly new to Apache Drill and im having troubles converting my Oracle specific sql scripts (pl/sql) to Drill based querys.
For example i have a Scripts who checks for processed data in the last X Days.
In this script im using the the sysdate function.
Here is my old script:
SELECT i.id,i.status,status_text,i.kunnr,i.bukrs,i.belnr,i.gjahr,event,i.sndprn,i.createdate,executedate,tstamp,v.typ_text,i.docnum,i.description, i.*
FROM in_job i JOIN vstatus_injob v ON i.id= v.id
WHERE 1=1
AND i.createdate > sysdate - 30.5
order by i.createdate desc;
When i looked up in terms of drill specific Datetime Diff functions i found "TIMESTAMPDIFF".
So here is my "drillified" script:
SELECT i.id, i.status, status_text, i.kunnr, i.bukrs, i.belnr, i.gjahr, i.event, i.sndprn, i.createdate, i.executedate, i.tstamp,v.typ_text,i.docnum,i.description,i.*
FROM SchemaNAME.IN_JOB i JOIN SchemaNAME.VSTATUS_INJOB v ON i.id=v.id
WHERE TIMESTAMPDIFF(DAY, CURRENT_TIMESTAMP, i.createdate) >=30
And the Error that is returned reads like this:
DATA_READ ERROR: The JDBC storage plugin failed while trying setup the SQL query.
By further inspection i can see the Oracle specific error that reads:
Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "TIMESTAMPDIFF": invalid ID
So now my question:
I thought apache drill replaces the function "TIMSTAMPDIFF" at runtime. But from what i can see in the logs its more like that Drill Hands over the Function Call "TIMESTAMPDIFF" to the Oracle database.
If thats true, how could i change my script to calculate the time difference (in days) and compare it to an int (ie 30 in the script).
If i use sysdate like above Apache Drill jumps in and says it doesnt know "sysdate".
How would you guyes handle that?
Thanks in advance and so long
:)
I have found a solution...
Just in Case someone (or even me in the future) is having a similar problem.
{
"queryType": "SQL",
"query": "select to_char(SELECT CURRENT_TIMESTAMP - INTERVAL XX MONTH FROM (VALUES(1)),'dd.MM.yy')"
}
With some to_char and the use of the CURRENT_TIMESTAMP - Interval Function Calls i can get everything i needed.
I took the query above packed it into an Grafana Variable, named it "timeStmpDiff" and then queried everything with an json Api Call to my Drill instance.
Basically:
"query" : "SELECT i.id, i.status, status_text, i.kunnr, i.bukrs, i.belnr, i.gjahr, i.event, i.sndprn, i.createdate, i.executedate, i.tstamp,v.typ_text,i.docnum,i.description,i.* FROM ${Schema}.IN_JOB i JOIN ${Schema}.VSTATUS_INJOB v ON i.id=v.id WHERE i.createdate >= '${timeStmpDiff}' order by i.createdate desc"
You can, of course query it in on go with an subselect.
But because i use grafana it made sense to me to bundle that in a Variable.

Openfire ORA-00907: Missing right parenthesis

I have the following problem retrieving messages from the OpenFire Monitoring Service plugin. I discovered that the error is due to an incorrect query in the database but I still cannot detect the error for which the query is not working correctly.
SELECT
fromjid,
fromjidresource,
tojid,
tojidresource,
sentdate,
body,
stanza,
messageid,
barejid
FROM
(
SELECT
DISTINCT ofmessagearchive.fromjid,
ofmessagearchive.fromjidresource,
ofmessagearchive.tojid,
ofmessagearchive.tojidresource,
ofmessagearchive.sentdate,
ofmessagearchive.body,
ofmessagearchive.stanza,
ofmessagearchive.messageid,
ofconparticipant.barejid
FROM
ofmessagearchive
INNER JOIN ofconparticipant ON ofmessagearchive.conversationid =
ofconparticipant.conversationid
WHERE
(
ofmessagearchive.stanza IS NOT NULL
OR ofmessagearchive.body IS NOT NULL
)
AND ofmessagearchive.messageid IS NOT NULL
AND ofmessagearchive.sentdate >= 0
AND ofmessagearchive.sentdate <= 1602748770287
AND ofconparticipant.barejid = 'usuario3#192.168.0.79'
AND (
ofmessagearchive.tojid = 'usuario4#192.168.0.79'
OR ofmessagearchive.fromjid = 'usuario3#192.168.0.79'
)
ORDER BY
ofmessagearchive.sentdate DESC
LIMIT
100
) AS part
ORDER BY
sentdate
I get an error when doing the following query
ORA-00907: missing right parenthesis
Command line error:32 Column: 9
There is no LIMIT keyword available in Oracle and if you are using Oracle 12c you can use FETCH FIRST 100 ROWS ONLY instead of it.
You cannot use AS to give alias to the sub query and it is not recognised by Oracle. So either you can remove the alias completely as you are not using it anywhere or just remove the AS and keep the alias name part only which should be fine.
Here is a good SO link about the Oracle limiting result set and you can always look into other sites available such as Oracle base or the official document as well. For 11g solution you have to use row_number

Reserved error (-7776) ms access

tried to run below query with sql pass through query to connect to oracle
SELECT distinct sa.request_date "RequestDate"
FROM xxx.yyyyyy sa
where to_char(sa.request_date, 'yyyy') = ‘2017’
--and rownum=1;
its fine when i put "and rownum=1" but when i ran the full query
i got below error:
please help, i have been busy with it for 3 days :(
Thanks!

Oracle DBLink issue (ORA-02020)

I am struggling to understand why I keep getting the ORA-02020: too many database links in use error, but only at some locations. Background: I have an Oracle 11g server at corporate that talks to Oracle 11g servers in 30 different locations. I run the following SQL statements in sequence talking to each location:
INSERT INTO rx_net
SELECT rxtx.dispensed_drug_ndc,
d.drug_name,
txtp.balance_due_from_tp
TRUNC (rxtx.reportable_sales_date) "DISPENSE_DATE",
TRUNC (rxtx.will_call_picked_up_date) "SOLD_DATE",
TRUNC (rxtx.return_to_stock_date) "CANCEL_DATE",
(dc1.cost_amount / d.pack) * rxtx.fill_quantity "ACQ_COST",
txtp.final_copay,
rxtx.fill_quantity "FILL_QTY",
txtp.balance_due_from_tp + txtp.final_copay "TOTAL_PRICE",
pt.bin_number,
rxtx.uc_price,
rx.rx_number,
LPAD (SUBSTR (ss.VALUE, 1, 3), 3, 0) "STORE_NUM",
rxtx.partial_fill_status "PARTIAL_FILL",
pt.pcn,
card.group_code,
pl.carrier_code,
(dc2.cost_amount / d.pack) * rxtx.fill_quantity "AWP"
FROM schema.rx_summary#store01 rx,
schema.rx_tx#store01 rxtx,
schema.tx_tp#store01 txtp,
schema.drug#store01 d,
schema.drug_cost_type#store01 dct1,
schema.drug_costs#store01 dc1,
schema.store_registration#store01 sr,
schema.patient_tp_link#store01 ptl,
schema.card#store01 card,
schema.plan#store01 pl,
schema.plan_transmit#store01 pt,
schema.store_settings#store01 ss,
schema.drug_cost_type#store01 dct2,
schema.drug_costs#store01 dc2
WHERE rx.id = rxtx.rx_summary_id
AND rxtx.id = txtp.rx_tx_id
AND rxtx.dispensed_drug_ndc = d.ndc
AND d.id = dc1.drug_id
AND dct1.id = dc1.drug_cost_type_id
AND ptl.id = txtp.patient_tp_link_id
AND card.id = ptl.card_id
AND pl.id = card.plan_id
AND pl.id = pt.plan_id
AND d.id = dc2.drug_id
AND dct2.id = dc2.drug_cost_type_id
AND dct1.cost_type = 'ACQ'
AND dct2.cost_type = 'AWP'
AND rxtx.will_call_picked_up_date BETWEEN '01-APR-17' AND '08-APR-17'
AND rxtx.nhin_store_id = sr.nhin_store_id
AND ss.name = 'storeDescription.storeNumber'
AND txtp.counter = 0
AND pt.site_code = 'ERX1'
ORDER BY rxtx.reportable_sales_date;
commit;
alter session close database link store01;
The SQL will execute (either in Toad or on the server in SQLPlus) fine for the first seven locations, but when it hits location 8 if fails with that error. I've had this happen on other SQL, either for all locations, or other 'breakpoint' locations. I've always just worked around it, but decided that now is the time to resolve the problem.
Today I decided to execute the SQL and run the command select * from v$dblink; after every "alter session..." statement, but before location 8 it shows "no rows selected," so it seems it isn't a delay between "alter session" and the dblink being shutdown. While the SQL is running, I will see 4 open sessions to a single location, so it makes me wonder if recursive calls to the same server is possibly the problem. I have also resisted changing the OPEN_LINKS setting greater than 4, because it feels like I'm masking the core problem.
Any ideas or wisdom would be greatly appreciated!

SQL query works even with wrong syntax

I am running a SQL query in stored procedure which is like following
SELECT
t1.id,t2.Name
FROM
table1 t1 , table2 t2 ,table2 t3,table4 t4
WHERE
t1.id=t3.t4.id
this query gets executed on SQL server 2008 when its compatible with SQL server 2000 but if we turn OFF the compatibility with SQL server 2000 then this Query gives syntax error which is expected.
Can some one help me to understand why this is happeneing ? thanks in advance
Original query:
SELECT
ConfigID , LocationDesc + '-' + LOBTeamDesc LocLOBTeamSource
FROM Config CONFIG , Location_LOBTeam LOCLOB , Location LOC , LOBTeam LOB, System SRC
WHERE CONFIG.LocationLOBTeamID = LOC.LOB.LocationLOBTeamID
AND CONFIG.SourceSystemID = SRC.SystemID
AND LOCLOB.LocationID = LOC.LocationID
AND LOCLOB.LOBTeamID = LOB.LOBTeamID
AND (GETDATE() BETWEEN CONFIG.effectiveDate AND CONFIG.EndDate
OR CONFIG.EndDate IS NULL)
ORDER BY
LOC.LocationCode
I think that original query, with current standard join syntax applied would be this:
SELECT
ConfigID
, LocationDesc + '-' + LOBTeamDesc LocLOBTeamSource
FROM Config CONFIG
INNER JOIN Location_LOBTeam LOCLOB
ON CONFIG.LocationLOBTeamID = LOCLOB.LocationLOBTeamID
INNER JOIN Location LOC
ON LOCLOB.LocationID = LOC.LocationID
INNER JOIN LOBTeam LOB
ON LOCLOB.LOBTeamID = LOB.LOBTeamID
INNER JOIN [System] SRC
ON CONFIG.SourceSystemID = SRC.SystemID
WHERE (GETDATE() BETWEEN CONFIG.effectiveDate AND CONFIG.EndDate
OR CONFIG.EndDate IS NULL)
ORDER BY
LOC.LocationCode
Perhaps this will help.
+EDIT
"System" as a table name, could that be a problem? Suggest you try it as [System]
+EDIT2
The original is given with this: LOC.LOB.LocationLOBTeamID but that appears to be an error as there is an alias LOCLOB
I think below post from msdn answers this issue Compatibility Levels and Stored Procedures
in the above post the point number 3 under section "Differences Between Compatibility Level 80 and Level 90" states "WHEN binding the column references in the ORDER BY list to the columns defined in the SELECT list, column ambiguities are ignored and column prefixes are sometimes ignored. This can cause the result set to return in an unexpected order."
on my database I am using compatibility level 80 i.e 2000 thats why it runs smoothly with the given syntax but when I remove this compatibility and make it to 100 i.e. 2008/R2 script gives syntax error which is expected