Pro*C syntax with select in select - syntax

Why won't this compile?
EXEC SQL insert into ssa (id )
select ( select max(id)
from ss s
where s.id = t.id )
from temp_sca t
where not exists
(select null
from sca ssa 2
where ssa2.ss_id = ( select max (id)
from ss s
where s.x = t.ss_id )
and ssa2.x = t.x )
Error I get:
select ( select max ( id)
...................1
PCC-D-02201, Encountered the symbol "max" when expecting one fo the following:
( ) * + ...
The symbol "(" was substituted for "max" to continue
Can we not embed a select in the select clause in Pro*C?
Oracle 11g on Solaris 10
Or is there a work-around?

Yep, converted the before mentioned to a dynamic sql c-string, then did an EXEC SQL EXECUTE IMMEDIATE on the resulting sql/c string, and all is well. Hopefully someone else may find this solution helpful.

MAX function is used here and MAX should be used with a GROUP BY. So, when Pro*C compiler tries to compile this, it does not find a GROUP BY and throwing this compilation error.

Related

Oracle Invalid Identifier (with Inner Join)

I'm having an "Invalid Identifier" in Oracle because of the "B.username" (username column does exist in USER table). When i remove this, it's working fine. How to resolve this issue? I came from a MySQL background.
SELECT * FROM (SELECT qNA.assignment, qNA.regDate, B.username, (
SELECT DISTINCT NVL(idx, 0)
FROM EK_USERGRADE
WHERE year = (SELECT DISTINCT userGradeNo FROM EK_USER WHERE ID = qNA.userIdx)
) AS userGradeIdx
FROM EK_NEWTESTAPPLICANT qNA
WHERE IDX = :idx ) A
INNER JOIN EK_USER B ON (A.userIdx = B.ID)
Let's try this with a simplified version of your query:
-- test tables
create table NEWTESTAPPLICANT as select 1 useridx from dual ;
create table B as select 1 id, 'name1' username from dual ;
-- query
select *
from (
select B.username
from NEWTESTAPPLICANT qNA
) A join B on A.useridx = B.id ;
-- ORA-00904: "B"."USERNAME": invalid identifier
There's no "username" column in the NEWTESTAPPLICANT table, which causes the error. A LATERAL inline view (examples see here) may do the trick ...
-- query
select
*
from B, lateral (
select B.username
from NEWTESTAPPLICANT qNA
) A ;
-- result
ID USERNAME USERNAME
1 name1 name1
This works with Oracle 12c.
The problem is, that both your virtual table A and users B have the same column name "username". Specify alias in the main select, like "Select A.* , B.* from(...".
Is it ORA-00903?
User is a reserved word are you sure you created this table? Table name cannot be a reserved word.

from_unixtime thinks data is string where data is int/bigint in hadoop hive

My code is as below..
SELECT
to_date(from_unixtime(time_first_touch)) AS sDate
FROM (
SELECT
MIN(GET_JSON_OBJECT(swanviraw.textcol,'$.ev_time')) as time_first_touch,
COUNT(*) as number_of_events
FROM swanviraw
) v
Its throwing an error- Error while compiling statement: FAILED: SemanticException [Error 10014]: Line 2:10 Wrong arguments 'time_first_touch': No matching method for class org.apache.hadoop.hive.ql.udf.UDFFromUnixTime with (string). Possible choices: FUNC(bigint) FUNC(bigint, string) FUNC(int) FUNC(int, string) [ERROR_STATUS]
Now, the point is the following query is working fine.. the ev_time has int/bigint values as MIN works perfectly in the following..
SELECT
MIN(GET_JSON_OBJECT(swanviraw.textcol,'$.ev_time')) as time_first_touch,
COUNT(*) as number_of_events
FROM swanviraw
Any help is sincerely appreciated..
Thanks
As GET_JSON_OBJECT returns json string, and as error indicates that from_unixtime expect int or bigint, you need to convert time_first_touch to bigint:
SELECT
to_date(from_unixtime(time_first_touch)) AS sDate
FROM (
SELECT
MIN(cast(GET_JSON_OBJECT(swanviraw.textcol,'$.ev_time') as bigint)) as time_first_touch,
COUNT(*) as number_of_events
FROM swanviraw
) as v
OR
SELECT
to_date(from_unixtime(time_first_touch)) AS sDate
FROM (
SELECT
MIN(unix_timestamp(GET_JSON_OBJECT(swanviraw.textcol,'$.ev_time'))) as time_first_touch,
COUNT(*) as number_of_events
FROM swanviraw
) as v

Mutiple Where subqueries in Hive doesn't work

I have a Query like below:
SELECT T.MTH_END_DT, T.SRC_SYS_CD, T.BTCH_ID
FROM PROD_RCRR.BAL_CNTRL_LOG T
WHERE T.SRC_SYS_CD='SL'
AND T.MTH_END_DT in (SELECT(MAX(MTH_END_DT)) FROM PROD_RCRR.BAL_CNTRL_LOG)
AND T.BTCH_ID in (SELECT(MAX(BTCH_ID )) FROM PROD_RCRR.BAL_CNTRL_LOG)
A error message shows Hive only can support one "in" clause. Anyone can give me a solution?
You can replace the whole thing with Join ON clause
SELECT
T.MTH_END_DT
, T.SRC_SYS_CD
, T.BTCH_ID
FROM PROD_RCRR.BAL_CNTRL_LOG T
JOIN ( SELECT
MAX(MTH_END_DT) ENDT
, MAX(BTCH_ID ) BTCH
FROM PROD_RCRR.BAL_CNTRL_LOG ) X
ON T.SRC_SYS_CD='SL'
AND T.MTH_END_DT = X.ENDT
AND T.BTCH_ID = X.BTCH

ORA-00928 selecting from view over DB link, but works locally

I have a view with a query of the following form which works perfectly when I query it on the local server from any schema:
WITH dates AS (
SELECT /*+ materialize */ ... FROM ( SELECT ... FROM table#link)
UNION ALL
SELECT * FROM ( SELECT /*+ materialize */ FROM table#link )
)
SELECT ... FROM (
SELECT ... FROM (
SELECT ... FROM (
SELECT ... FROM (
SELECT ... FROM
)
) foo
LEFT OUTER JOIN (
SELECT /*+ USE_MERGE(hle dates) */ ... FROM
) bar ON conditions
)
)
UNION ALL
SELECT ... FROM (
SELECT ... FROM (
SELECT ... FROM (
SELECT ... FROM (
SELECT ... FROM
)
) foo
LEFT OUTER JOIN (
SELECT /*+ USE_MERGE(hle dates) */ ... FROM
) bar ON conditions
)
)
When I run the query from any remote db link on any other server, e.g. SELECT * from someschema.my_view#db_link, I get:
ORA-00928: missing SELECT keyword
ORA-02063: preceding line from PLLDB
00928. 00000 - "missing SELECT keyword"
*Cause:
*Action:
Error at Line: 2 Column: 9
Oracle thinks line 2 is a problem. Here are the first five actual lines:
WITH dates AS (
-- Get days
SELECT /*+ materialize */
row_number() OVER (ORDER BY begin_period DESC) rn,
'D' AS interval_type,
All other views work perfectly over the DB link (once edited to work around any related Oracle bugs).
Why does this view work perfectly locally but not over a db link?
Referring from Here
BUG 768478 The message "ORA-00928: missing SELECT keyword" can occur when Oracle cost-based optimization attempts to rewrite a query that contains a set operator (e.g. UNION, MINUS, or INTERSECT) with a materialized view.
There are three workarounds:
1. Disable query rewrite with an "ALTER {SESSION|SYSTEM} DISABLE QUERY REWRITE" statement.
2. Use a NOREWRITE hint with all SELECT statements referenced by the set operator.
3. Use a REWRITE(mv) hint with all SELECT statements in the set operator to tell the optimizer to explicitly use a materialized view.
Scalar expressions referencing SQL factoring elements ( WITH ... AS ) are not fully supported within view's subqueries. Acessing it locally work fine but remote access via dblinks errors out every time.

ORA-01791 Pl-Sql error

hi guy i have a query that give me the followin error:
ORA-01791: not a SELECTed expression
this is the select expresison , please can you tell me why ?
declare
freqLettura varchar2(64);
billingcy varchar2(64);
begin
freqLettura := null;
billingcy := null;
for rec in ( select distinct(fn_get_facilityid(z.uidfacility) ) as a, 1 as b
from facilityhistory z,
locality l ,
plant p ,
ztmp_sam_tb_sdv zsdv ,
ztmp_sam_tb_plantcode zplant ,
sam_tb_ca_pdr sam,
meterhistory mh,
meter m ,
meterclass mc
where
Z.UIDLOCALITY = L.UIDLOCALITY and
p.UIDPLANT = L.UIDPLANT and
z.uidaccount = zsdv.uidaccount and
p.plantcode = zplant.plantcode and
sam.uidfacility = z.uidfacility and
z.stoptime is null and
sam.status = 'U' and
mh.uidfacility = z.uidfacility and
mh.uidmeter = m.uidmeter and
m.uidmeterclass = mc.uidmeterclass and
(billingcy is null or p.UIDBILLINGCYCLE = billingcy )
AND
(
(
(freqLettura = 'G') AND ( mh.corrmeterid is not null and mh.stoptime is null and mc.maxflowmeter >= SAM_FN_GET_PARAMETER_FLOAT('MAXFLOWMET_DETT_GIORN'))
)
OR
(
nvl(freqLettura,'nullo') <> 'G' AND (freqLettura is null or sam.readfrequency = freqLettura)
)
) and ROWNUM = 1 order by sam.stoptime, sam.uidsamtbpdr desc ) loop
begin
insert into ztmp_sam_tb_elab_pdr (facilityid, uidbatchrequest) VALUES (rec.a, rec.b);
exception
when dup_val_on_index then
null;
end;
end loop;
end;
Whenever you get an Oracle error message you don't understand, the first thing to do is look up the meaning. One way is simply to Google it. In this case the full description found in
Oracle9i Database Error Messages is:
ORA-01791 not a SELECTed expression
Cause: There is an incorrect ORDER
BY item. The query is a SELECT DISTINCT query with an ORDER BY clause.
In this context, all ORDER BY items must be constants, SELECT list
expressions, or expressions whose operands are constants or SELECT
list expressions.
Action: Remove the inappropriate ORDER BY item from the SELECT list
and retry the statement.
(Oddly this error message isn't documented in the 10G or 11G manuals, despite still being raised!)
This matches the statement you have written, which is a SELECT DISTINCT query where you are trying to order the results by a column that you did not select.
If you think about it, what you are asking for doesn't make sense: by selecting DISTINCT values that do not include sam.stoptime (for example) you may be consolidating many rows with different values for sam.stoptime, so which one would govern the ordering?
Also, as Noel's answer points out, there is no reason to have an ORDER BY clause in this code anyway, so the solution is simply to remove it.
If you are using DISTINCT in your SELECT query, then your ORDER BY clause should contain only those columns that your selecting. In this case sam.stoptime, sam.uidsamtbpdr are not there in SELECT statement. You can remove the ORDER BY clause, as it is not doing anything useful in your example.

Resources