Getting invalid identifier SQL query - oracle

I am trying to execute next query:
MERGE INTO NOTIFICATION_OBJS p
USING (SELECT
:fcsNotif_id as doc_id,
:OKPD2_code as OKPD2_code,
:OKPD2_name as OKPD2_name,
:quantity_value as quantity,
:purchaseObject_price as price
FROM DUAL
) v
ON (p.doc_id=v.doc_id)
WHEN MATCHED THEN
UPDATE SET
p.OKPD2_code = v.OKPD2_code,
p.OKPD2_name = v.OKPD2_name,
p.quantity_value = v.quantity_value,
p.price = v.price
WHEN NOT MATCHED THEN
INSERT (p.doc_id, p.OKPD2_code, p.OKPD2_name, p.quantity_value, p.price)
VALUES(v.doc_id, v.OKPD2_code, v.OKPD2_name, v.quantity_value, v.price)
I am sending to bind method next dictionary:
{'OKPD2_code': '62.02.30.000', 'OKPD2_name': 'some text', 'purchaseObject_price': '20466982.25', 'quantity_value': '1', 'fcsNotif_id': '18941152'}
But I am getting error:
ORA-00904: "P"."OKPD2_NAME": invalid identifier
All other query with binding are working. Please help me to find error.

There's no OKPD2_NAME column in NOTIFICATION_OBJS table.
If you used double quotes while creating that table (and its columns), you should
recreate the table without double quotes, or
reference the table (and its columns that use mixed case) using double quotes again, specifying case EXACTLY as it was while creating the table
[EDIT, after the screenshot has been uploaded]
Column name really is created using mixed case, so you'll have to reference it exactly like that: "OKPD2_name" paying attention to double quotes and mixed case.
If you use "okpd2_name" or "okPD2_NAME" or anything but "OKPD2_name", it won't work. Once again: get rid of double quotes.

Related

ADF SQL Query with single quotes around dates

I'm trying to do a simple pull from oracle based on a date. Eventually, I need this query to be dynamic hence using #concat() function in ADF. For simplicity sake I want to run the below oracle query in ADF with concat.
Since oracle requires single quotes I'm trying to escape it by using two single quotes.
oracle query:
select * from schema.customer where updated > to_date('06/25/2019','MM/DD/YYYY')
here's my ADF text:
#concat('select * from iris.iris_customer where updated >
to_date(','''06/25/2019''','''MM/DD/YYYY''',');')
I'm using to_date because I was getting the 'not a valid month' error and thought I could resolve it. Here's the error I'm getting:
{
"errorCode": "2200",
"message": "Failure happened on 'Source' side. ErrorCode=UserErrorUnclassifiedError,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=Odbc
Operation
Failed.,Source=Microsoft.DataTransfer.ClientLibrary.Odbc.OdbcConnector,''Type=System.Data.Odbc.OdbcException,Message=ERROR
[22008] [Microsoft][ODBC Oracle Wire Protocol
driver][Oracle]ORA-01843: not a valid month,Source=msora28.dll,'",
"failureType": "UserError",
"target": "copy_to_adl" }
you should use '' for escaping single quote.
My Example: #{concat('DELETE FROM MyTable WHERE [ColumnDate] LIKE','''',pipeline().parameters.processYear,pipeline().parameters.processMonth,'%','''')}
You can also use this SQL Syntax inside Expression builder in ADF Data Flows to escape single quotes:
"SELECT * FROM myTable WHERE Id = {$myVar} AND Gender = 'M'"

Why do I get "ORA-00932: inconsistent datatypes: expected - got -" when using COLLECT() in a prepared statement?

I am using this query with the Perl DBI:
SELECT c.change_id
, COLLECT(t.tag) AS the_tags
FROM changes c
LEFT JOIN tags t ON c.change_id = t.change_id
WHERE c.project = ?
GROUP BY c.change_id
The DBI uses OCI to prepare this statement, bind the value I pass, and get the results. But Oracle, for some reason, does not like it. The error output is:
ORA-00932: inconsistent datatypes: expected - got - (DBD ERROR: error possibly near <*> indicator at char 41 in '
SELECT c.change_id
, <*>COLLECT(t.tag) AS the_tags
FROM changes c
LEFT JOIN tags t ON c.change_id = t.change_id
WHERE c.project = :p1
GROUP BY c.change_id
'
Not very informative. However, I can make this error go away not only by changing the call to COLLECT() also by replacing the placeholder with the actual value:
SELECT c.change_id
, COLLECT(t.tag) AS the_tags
FROM changes c
LEFT JOIN tags t ON c.change_id = t.change_id
WHERE c.project = 'tryoracle'
GROUP BY c.change_id
That version works perfectly. Why doesn't Oracle like the prepared statement with the COLLECT()?
In case it's any help, here is a trace of the OCI-related calls extracted via ora_verbose = 6 (h/t #bohica).
Finally got a solution to this issue, thanks to some digging by a user. The problem was not with the placeholder; why it worked without the placeholder on the VirtualBox image I have no idea. No, the issue was with the COLLECT(). Seems that both the values being collected need to be cast to a specific type, and the resulting array also needs to be cast to a pre-defined array data type. Just so happens that my code has a custom array type:
CREATE TYPE sqitch_array AS varray(1024) OF VARCHAR2(512);
So I'm able to get the query to work by casting the COLLECT() like so:
CAST(COLLECT(CAST(t.tags as VARCHAR2(512))) AS sqitch_array)

Oracle: Invalid identifier

Can anyone explain to me why I get a 00904 error when I run the following
SELECT "OASM"."DT_GROUPEPG".GROUPEPGID,
"OASM"."DT_GROUPEPG".GROUPID,
"OASM"."DT_GROUPEPG".EPGID,
"OASM"."DT_GROUPEPG".ZAPID,
"OASM"."LU_EPG".LASTREADTIME,
"OASM"."LU_EPG".SERVICE_NAME,
"OASM"."LU_EPG".SOURCE_ID,
"OASM"."LU_EPG".ONID,
"OASM"."LU_EPG".TSID,
"OASM"."LU_EPG".SID,
"OASM"."LU_EPG".TYPE_ID,
"OASM"."LU_EPG".OPERATOR_ID,
"OASM"."LU_EPG".URL
FROM "OASM"."DT_GROUPEPG"
INNER JOIN "OASM"."LU_EPG"
ON "OASM"."DT_GROUPEPG".EPGID = "OASM"."LU_EPG".EPGID
ORDER BY LastReadTime;
I'm still new to Oracle, and was of the impression that, because Oracle executes blocks of statements, and not line by line, that doing this kind of query would be valid? The error currently fires at the OPERATOR_ID line, but removing/commenting it out just moves the erro up a line, until all the LU_EPG table references are removed
You can't (and don't need to) specify the schema name when referring to the columns. Also, I recommend you use table aliases (e.g. a and b in the example below):
SELECT a.GROUPEPGID,
a.GROUPID,
a.EPGID,
a.ZAPID,
b.LASTREADTIME,
b.SERVICE_NAME,
b.SOURCE_ID,
b.ONID,
b.TSID,
b.SID,
b.TYPE_ID,
b.OPERATOR_ID,
b.URL
FROM "OASM"."DT_GROUPEPG" a
INNER JOIN "OASM"."LU_EPG" b
ON a.EPGID = b.EPGID
ORDER BY b.LastReadTime;

Can't find the cause of a 'missing right parenthesis' error

I am having problems with a line of code. I am trying to create a count function for a view that I created. I have done this a bunch of different ways but below is the format that I have most recently used.
CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, COUNT(RIDERS) AS
SELECT EVENTNAME, RACES.DESCRIPTION,
RIDERS_FIRSTNAME||' '||RTRIM(RIDERS_LASTNAME)
FROM EVENTS, RACES, PARTICIPATION, RIDERS
WHERE EVENTS.EVENTID = RACES.EVENTID
AND RACES.RACEID = PARTICIPATION.RACEID
AND RIDERS.RIDERID = PARTICIPATION.RIDERID
ORDER BY RIDERS.RIDERS_LASTNAME, EVENTNAME;
The error I am getting is ORA-00907: missing right parenthesis. The error is at the (COUNT(RIDERS) part of the code. Any ideas how I should tackle this?
The list of names in parentheses on line 1 should be the names of the view columns:
CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, COUNT(RIDERS) AS ...
You can't create a column called "COUNT(RIDERS" or even "COUNT(RIDERS)" since a column name may not contain ( or ). This would work:
CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, RIDER_FULL_NAME) AS ...
However it appears that you really do want a count of something, though I'm not sure of what. To do that the view definition would have to be something like:
CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, RIDER_COUNT) AS
SELECT EVENTNAME, RACES.DESCRIPTION, COUNT(*)
FROM EVENTS, RACES, PARTICIPATION, RIDERS
WHERE EVENTS.EVENTID = RACES.EVENTID
AND RACES.RACEID = PARTICIPATION.RACEID
AND RIDERS.RIDERID = PARTICIPATION.RIDERID
GROUP BY EVENTNAME, DESCRIPTION;
(i.e. the COUNT function goes in the SELECT part, not in the list of column names).
As an aside, since you are presumably new to Oracle, I would suggest you start using the more modern ANSI join syntax to make your queries clearer:
...
FROM EVENTS
JOIN RACES ON RACES.EVENTID = EVENTS.EVENTID
JOIN PARTICIPATION ON PARTICIPATION.RACEID = RACES.RACEID
JOIN RIDERS ON RIDERS.RIDERID = PARTICIPATION.RIDERID
CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, COUNT(RIDERS) AS
....
Should not it be:
CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, COUNT(RIDERS)) AS
As the error message points out you are missing a closing bracket ). The bracket is opened here : 'ERP_REPORT(EVENTNAME' and is never closed.
you can't have brackets in column names unless you put quotation marks around. Try this:
CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, "COUNT(RIDERS)") AS ...
or
CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, COUNT_RIDERS) AS ...
For example:
SQL> CREATE OR REPLACE VIEW foo ("count(*)") AS SELECT COUNT(*) FROM dual;
View created
SQL> CREATE OR REPLACE VIEW foo (count_all) AS SELECT COUNT(*) FROM dual;
View created
As Tony points out there are actually several syntax errors in your statement. The missing bracket is just the first.
I find it useful to have an IDE which supports bracket matching, because it can be hard to walk back through the code and find which bracket lacks a mate. As it happens my choice is TextPad but just about anything which is more advanced than NotePad ought to be able to do this.

CodeIgniter: Unknown column xxx in ‘where clause’

I am working on a bug(?) for a few hours now, but couln’t fix it.
This is my code:
if(!$this->db->get_where('merken',array('m_merken' => $brand))->count_all_results()){
$insetData = array('m_name' => $brand);
$this->db->insert('merken', $insetData);
}
$brand contains ‘Acer’ in this preview.
A Database Error Occurred
Error Number: 1054
Unknown column ‘Acer’ in ‘where clause’
SELECT * FROM (`merken`) WHERE `m_name` = Acer
I want to check if it already exists, but it won’t work very well.
Without quotes, your statement:
SELECT * FROM (`merken`) WHERE `m_name` = Acer
Acer refers to a column-name. If your intent is a string literal, put it in single-quotes, as in:
SELECT * FROM (`merken`) WHERE `m_name` = 'Acer'
Also, as a matter of good programming practice, avoid SELECT *, better to SELECT each column you want to return, even if the list is lengthy.
-- EDIT --
I suspect I'm missing the point... the SQL is generated. Two things to check:
Is m_name correctly declared as a string/varchar/char field?
Failing that, try literally setting the brand name to 'Acer', with the quote marks. I doubt this is a reasonable solution, though.

Resources