SQL ORA 935: Missing Expression - sql

I have gone over the SQL for an hour and can't find why the error is being raised. I have checked all the basic reasons this error can occur and found nothing. I'm suspicious of the CASE statement but it appears to be correct. Can anyone spot the problem or point me in a direction? Thanks
INSERT INTO RPT_HOUSEHLDBATCH
(CUSTOMERKEY,HOUSEHOLDNBR,CUSTOMERTYPE,LASTNAME,FIRSTNAME,ADDRNBR,AddressLine1,AddressLine2,AddressLine3,
CITYNAME, STATECD, ZIPCD, SCORE, DATECREATED, RUNDATE, TYPECD, PREVIOUSHHLDNBR)
SELECT CustomerKey,' || in_HHNbr || ',
CASE SUBSTR(CUSTOMERKEY,1,1)
WHEN ''P'' THEN ''I''
WHEN ''O'' THEN ''B''
END CASE,
a.LastName,
a.FirstName,
AddrNbr,
AddressLine1,
AddressLine2,
AddressLine3,
Cityname,
StateCd,
ZipCd,
2, b.AddDate, SYSDATE, ''' || in_NewUpd || ''', HouseHoldNbr
FROM rpt_HouseHldBatchwrk a
JOIN PERS b
ON SUBSTR(a.CUSTOMERKEY,2) = b.PersNbr
WHERE CUSTOMERKEY = ''P' || in_PersNbr || '''
UNION
SELECT CustomerKey,' || in_HHNbr || ',
CASE SUBSTR(CUSTOMERKEY,1,1)
WHEN ''P'' THEN ''I''
WHEN ''O'' THEN ''B''
END CASE,
a.LastName,
a.FirstName,
AddrNbr,
AddressLine1,
AddressLine2,
AddressLine3,
Cityname,
StateCd,
ZipCd,
2, b.AddDate, SYSDATE, ''' || in_NewUpd || ''', HouseHoldNbr
FROM rpt_HouseHldBatchwrk a
JOIN ORG b
ON SUBSTR(a.CUSTOMERKEY,2) = b.OrgNbr
WHERE CUSTOMERKEY = ''O' || in_OrgNbr || '''

A good strategy to debug such a statement is to pare it down, as #OldProgrammer suggested.
In your case, I'd try to ignore the INSERT and get the SELECT running first.
As it's a UNION of two SELECTs, I'd split them into separate statements, too.
The in_NewUpd and in_PersNbr look strange, just like parameters in a procedure. I'd replace them with fixed known values, like WHERE CUSTOMERKEY LIKE ''P1234''
And please, please don't store SQL in a variable and execute it based on a condition. The syntax for that is to use placeholder like ? in the SQL instead of string concatenation ||. You'll flood the cursor cache if you do it wrong.

Turns out the in_HHNbr in some cases was coming in as a null. Concatenating null throws missing expression.
As far as how the code is structured I inherited this code and can't make major changes. I was sent to bug fix.

Related

Can i use group_concat inside another group_concat in SQL?

So i have a query like this:
select
p.identifier,
GROUP_CONCAT(
'[' ||
'{"thumbnail":' ||
'"' ||
ifnull(s.thumbnail,'null') ||
'"' ||
',"title:' ||
'"' ||
s.title ||
'","content": [' ||
GROUP_CONCAT(
'{"text":' ||
ifnull(c.text,'null') ||
'", "image":' ||
ifnull(c.image,'null') ||
'", "caption": "' ||
ifnull(c.caption,'null') ||
'"},'
) ||
']},'
)
from pois as p
join stories as s on p.identifier = s.poiid
join content c on s.storyid = c.storyid
group by s.storyid
And i got and error:
in prepare, misuse of aggregate function GROUP_CONCAT()
To see clearly i have a big object named POIS every POI have multiple STORIES and every STORY have multiple CONTENTS, and i want to display x rows(how many pois i have) and inside the column have every story that is connected to their poi(and every content inside stories) and i need this in json format so i can parse the database query and read back into my object.
I hope its clear what is my problem and you can help me.
So i changed the query to something like this:
SELECT p.identifier, (
SELECT json_group_array(json_object('id', s.storyid))
FROM stories AS s
WHERE s.poiid=p.identifier
) as stories,
(
SELECT json_group_array(json_object('id', c.contentid, 'storyId', s.storyid))
FROM content AS c
JOIN stories AS s ON c.storyid=s.storyid
WHERE s.poiid=p.identifier
) as contents
FROM pois AS p
GROUP BY p.identifier
this is my result:
enter image description here
but the 3 rd column i would like to put inside the second(every pois have multiple stories and every story have one or multiple contents, so the contents should be inside their stories.

Oracle SQL: Using CHR() function with || concatenate and join

Query below returns error
SELECT 'mailto:'|| fscp.parameter_value || '?subject=' || wfn.subject nid_subject || chr(38)
FROM apps.wf_notifications wfn, apps.fnd_svc_comp_param_vals_v fscp
WHERE fscp.component_id = :component_id
AND component_parameter_id = :param
AND wfn.item_key = :itemkey;
Error
ORA-00923: FROM keyword not found where expected
00923.00000 - "FROM keyword not found where expected"
When I remove the '|| chr(38)' at the end of the select statement, the query runs fine.
Something related to joining tables? Because the below query also works fine:
select 'Text: '||chr(39)||wfn.notification_id||chr(39) from wf_notifications wfn;
You have this in the select:
|| wfn.subject nid_subject ||
Perhaps you intend:
SELECT 'mailto:'|| fscp.parameter_value || '?subject=' || wfn.subject || nid_subject || chr(38)
----------------------------------------------------------------------^
Alex is right. The key in the question is that it works without chr(38). So, try this:
SELECT ('mailto:'|| fscp.parameter_value || '?subject=' || wfn.subject || chr(38) ) as nid_subject
Notice the use of parentheses and as to make it clear that a column alias is being defined.

multiple IS NOT NULL with OR operator

Does anyone know how to improve the below oracle sql query with multiple IS NOT NULL with OR operator:
select count(1)
from s_srv_req sr, s_evt_act act, s_bu bu
where sr.row_id = act.sra_sr_id(+)
and sr.bu_id = bu.row_id
and sr.last_upd > to_date('31-DEC-2013','DD-MON-YYYY')
and **(X_REASON_CODE1 is not null
OR X_REASON_CODE2 is not null
OR X_CONCERN_CODE1 is not null
OR X_CONCERN_CODE2 is not null
OR X_COMPONENT_CODE is not null)**
The purpose here is to fetch all records even if one of the codes column is not null.
Note: This query is taking much time and i cannot progress with such time taking queries. Thanks in advance.
You should use COALESCE function
select count(1)
from s_srv_req sr, s_evt_act act, s_bu bu
where sr.row_id = act.sra_sr_id(+)
and sr.bu_id = bu.row_id
and sr.last_upd > to_date('31-DEC-2013','DD-MON-YYYY')
and COALESCE(X_REASON_CODE1, X_REASON_CODE2, X_CONCERN_CODE1, X_CONCERN_CODE2, X_COMPONENT_CODE) is not null
have you tried with using a excluding method?
Use the total table to minus the records that all of those columns are null at the same time?
here are some fake code:
Method1, using Minus
table a
minus
table a with X_REASON_CODE1 ||
OR X_REASON_CODE2 ||
OR X_CONCERN_CODE1 ||
OR X_CONCERN_CODE2 ||
OR X_COMPONENT_CODE is not null
Method 2, using NOT EXISTS or NOT IN
table a
not exists X_REASON_CODE1 ||
OR X_REASON_CODE2 ||
OR X_CONCERN_CODE1 ||
OR X_CONCERN_CODE2 ||
OR X_COMPONENT_CODE is not null

firebird trigger string concatenation

anyone can help me with this. I created a trigger in firebird. I have this line that uses concatenation.
NEW.FIELDNAME = FIELD1 || FIELD2;
but it is not working, no record has been inserted. Is there any way for string concatenation? THANKS IN ADVANCE!
here is my full trigger
*query that initiate the variable PREV_STATUS:
SELECT FIRST 1 PREV_STATUSPERMINS FROM C3_EQUIPTSTATEPERMIN
WHERE TESTERID = NEW.TESTERID ORDER BY DATEMODIFIED DESC INTO :PREV_STATUS;
IF(PREV_STATUS IS NULL) THEN
BEGIN
NEW.PREV_STATUSPERMINS = '000';
END
ELSE
BEGIN
NEW.PREV_STATUSPERMINS = PREV_STATUS || NEW.STATUS;
END
You should use the "new" keyword to access the values of your fields within a trigger :
new.fieldname = new.field1 || new.field2;
Also, don't forget that if either of the fields is null, the concatenation will be null. If you want to avoid that, you could do something like this :
NEW.FIELDNAME = coalesce(new.FIELD1, '') || coalesce(new.FIELD2, '')

ORA-00907: missing right parenthesis in string query

I am not familiar with Oracle syntax, but I am getting a missing right parenthesis error when I am passing this string to an .Net Oracle Command and then it tries to fill the data adapter.
string cT = "SELECT 'PRODUCT' AS ItemType, 'x' || CAST(LPROD.QUANTITY AS VARCHAR2(50)), " +
"PNAME.NAME || ' (' || CAST((PRODS.\"SIZE\" AS VARCHAR2(50))) || ' ' || PRODS.MEASURE || ')' AS Name, " +
"PRODS.PRODUCT_ID as ProductSizeID, PRODS.UPC_CODE as BarCode FROM ORDERS ORDR, LI_PROD LPROD, PRODUCT_NAME PNAME, " +
"PRODUCT PRODS WHERE ORDR.INV_NUM = :Invoice_Num AND ORDR.ORDER_NUM = LPROD.ORDER_NUM " +
"AND LPROD.PRODUCT_ID = PRODS.PRODUCT_ID AND PRODS.PRODUCT_NAME_ID = PNAME.ID";
Can anyone spot the error that may be causing this? Thanks for your help.
Try:
cast(prods."SIZE" as varchar2(50))
Instead of:
cast((prods."SIZE" as varchar2(50)))
I recently came up with the exact same issue and could not find a solution. What ended up happening was that I had the following code from SQLServer that I was attempting to convert to Oracle.
CAST(col AS NVARCHAR(250)) AS col_name,
I was able to fix it by changing it to
CAST(col AS NVARCHAR2(250)) AS col_name,