Sql query not executing - showing invalid identifier error - sql

with ticket_orders AS(
select REGEXP_SUBSTR(st.ORDER_ID, '\d{3}-\d{7}-\d{7}') "new_order_id"
from
(select
REGEXP_SUBSTR(details, 'Order\s+ID(\s*\(s\))?:\s*\d{3}-\d{7}-\d{7}') AS "ORDER_ID"
from o_remedy ort
) st)
select new_t.new_order_id from ticket_orders new_t;
I'm trying to execute the following sql statement but it keeps showing error as "NEW_T"."NEW_ORDER_ID": invalid identifier
How to get this "new_order_id" because I need to join it further.

Either remove double quotes from the derived column name, or use double quotes in select clause as well.
select new_t."new_order_id" from ticket_orders new_t;
By default, Oracle convert every object name to upper case, unless you specify it in double quotes. So "new_order_id" will not be converted to upper case, but while selecting new_t.new_order_id will be converted to upper case, which doesn't exist.
To resolve it, either remove double quotes from the derived column name, or use double quotes in select clause as well.
I replicated it like this on my local machine.
This will not work.
with tbl as
(select empno "new_emp_no" from emp)
select t.new_emp_no From tbl t
And give
ORA-00904: "T"."NEW_EMP_NO": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 116 Column: 10
But this will work.
with tbl as
(select empno "new_emp_no" from emp)
select t."new_emp_no" From tbl t

WITH ticket_orders AS
(
SELECT
REGEXP_SUBSTR(st.ORDER_ID, '\d{3}-\d{7}-\d{7}') new_order_id
FROM
(
SELECT
REGEXP_SUBSTR(details, 'Order\s+ID(\s*\(s\))?:\s*\d{3}-\d{7}-\d{7}') AS ORDER_ID
FROM o_remedy ort
) st
)
SELECT new_t.new_order_id FROM ticket_orders new_t;

Related

Oracle SQL NVL() Function

Ques : How do I will select full table with "NVL()" Oracle SQL function
Code :
SELECT NVL(ORDERNO, 'AAA') * FROM GNGRB.PRBOOK;
Error :
ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
Error at Line: 11 Column: 30
Is this what you want?
SELECT NVL(ORDERNO, 'AAA') as NEWORDERNO, p.* FROM GNGRB.PRBOOK p;
This selects all rows and columns of the table, and adds another column, called ORDERNO, that replaces null values in ORDERNO with 'AAA'.
Note that this only works as long as ORDERNO is of a string-like datatype: both arguments of NVL() must have the same datatype.

ORA-00904: "ROW_ID": invalid identifier

I wanted to get the rowId from the below query, but when I execute the query I get an error.
WITH DATA_TBL AS
(SELECT
/*+ parallel(scr,10)
parallel(el,10) */ scr.rowid AS "row_id", scr.*
FROM SOURCE_TABLE scr
LEFT OUTER JOIN CITY el ON (el.CITY_NAME = scr.SUN_W)
WHERE el.rowid IS NULL
AND scr.SUN_W IS NOT NULL
AND GREATEST(scr.SUN_WA, scr.B ) IS NULL
)
SELECT row_id FROM DATA_TBL;
When I execute the above query I get the below error, I am not sure what I am doing wrong.
ORA-00904: "ROW_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
But the query runs without any issue if I do the below, but I wanted to select only row_id.
Do not use double quotes for identifiers! By default, Oracle upper cases all identifiers. You have explicitly defined it to be lower-case.
You could say select "row_id", but that is silly. Remove the double quotes in the CTE.

Oracle Pivot Multi Table

I am trying to create a Pivot in Oracle. I keep getting the error message
ORA-00904: "VALUEZ": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 18 Column: 6
Any thoughts?
SELECT * FROM
(
SELECT ACC.NBR,CTA.NAMEZ
FROM ACCS ACC
JOIN CARS CAR ON CAR.CAR_AAD_ID = ACC.ACC_AAD_ID
JOIN CTAS CTA ON CAR_CUS_ID = CTA_CUS_ID
)
PIVOT
(
MAX(VALUEZ) --comes from table CTAS (ERROR LINE)
FOR NAMEZ IN ('1','2','3') --from table CTAS
)
ORDER BY ACC.NBR;
As a side note, I would love it if it was possible to turn the ('1','2','3') into a subquery, but it looks like that is not possible from other post i have read. If it was easy it would be (select distinct namez from CTAS)
The columns referenced in your PIVOT clause must exist in the row source that is being pivoted. You select ACC.NBR,CTA.NAMEZ from the table; it looks like you need to expand that to ACC.NBR,CTA.NAMEZ,CTA.VALUEZ.
You cannot use a subquery to replace the list of pivot values. I believe the underlying reason for this is that the parser must be able to figure out the columns that the query will produce prior to executing it; so the pivot values must be hardcoded.
What you might be able to do, if it is appropriate to wrap this query up in a procedure or function, is to first execute a query to get the list of pivot values, then build the pivot query string using that information and execute it via dynamic SQL.
ora-01748: only simple column names allowed here pivot
select* from (SELECT TRUNC(I.POST_DATE) DATES FROM INVOICE I
INNER JOIN INVC_TENDER_V T ON T.INVC_SID=I.INVC_SID)
PIVOT
(COUNT(*)
FOR T.TENDER_TYPE IN(0,1) )

Use as in select statement on select statement

I have a request with a select statement that works in Oracle, but when I execute it in SQL Server it throws an exception; the request is :
SELECT non_existant FROM
(SELECT rownum AS non_existant ,cab, validite FROM tmp_rapprochement)
WHERE validite like '%non_existant%'
The error is :
Msg 207, Niveau 16, État 1, Ligne 2 Nom de colonne non valide : 'rownum'.
Thank you.
The error says that rownum is not a valid column name (I'm glad Google translate is there to help)
Msg 207, Level 16, State 1, Line 2 Invalid column name 'rownum'.
You need to fix that error first - make sure that all columns from the select's list are defined in the table.
Once you get that out of your way, you would need to provide an alias for the inner select, like this:
SELECT non_existant FROM
(SELECT rownum AS non_existant ,cab, validite FROM tmp_rapprochement) x
-- x above is an alias, it is mandatory in SQL Server syntax.
WHERE validite like '%non_existant%'
EDIT It appears that you are porting this query from Oracle, so rownum is not a real column. In this case you should replace it with a row_number function, like this:
SELECT non_existant FROM
(SELECT row_number() OVER (Order by cab) AS non_existant
, cab -- ^^^ Put rapprochement's primary key there
, validite FROM tmp_rapprochement
) x -- x is an alias, it is mandatory in SQL Server syntax.
WHERE validite like '%non_existant%'

Missing Expression when selecting all columns and one more

This kind of query would work perfectly in SQL Server, but it does not work in Oracle.
select issueno, * from SOMETABLE;
The error message I'm getting is:
ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action: Error at Line: 1 Column: 16
What is wrong?
Try this, when working with oracle db you need alias when you use column name along with *
select issueno, A.* from SOMETABLE A;
On Oracle you have to include the table name or an alias to use the *. Try this:
select issueno, SOMETABLE.*
from SOMETABLE;