Hi
In below query i am getting error Incorrect Syntax near = when i am using Stuff statement. I am using this query in Sap B1 Hana version.
SELECT
A."WhsCode",A."ItemCode",A."Description",A."Uom",A."Batch No.",A."Bin No"
From (
SELECT
T1."WhsCode",T1."ItemCode",Max(T2."ItemName") As "Description",
Max(T2."InvntryUom") As "Uom",T1."BatchNum" As "Batch No.",
(Select 'BinNo' := STUFF(( Select ',' + (A3."BinCode") from OBBQ A1 INNER JOIN OBTN A2 ON A1."SnBMDAbs" = A2."AbsEntry"
inner join OBIN A3 on A3."WhsCode" = A1."WhsCode" and A3."AbsEntry" = A1."BinAbs"
where A1."ItemCode" = T1."ItemCode" and A1."WhsCode" = T1."WhsCode" and A2."DistNumber" = T1."BatchNum" and A1."OnHandQty" > 0 for XML PATH('')),1,1,'')) As "Bin No",
FROM IBT1 T1
LEFT JOIN OITM T2 ON T2."ItemCode" = T1."ItemCode"
GROUP BY T1."WhsCode", T1."ItemCode", T1."BatchNum"
HAVING
SUM(CASE WHEN T1."Direction" = 0 THEN T1."Quantity" ELSE 0 END) -
SUM(CASE WHEN T1."Direction" = 1 THEN T1."Quantity" ELSE 0 END) > 0
) A
Thanks
Muhammad Imran AnsariPosted Feb 17, 2025, 5:29 AM
Hello Ramco,
The error you're encountering,
Incorrect Syntax near =, is likely due to the use of the:=operator in yourSTUFFstatement. In SQL, the:=operator is not standard and is not supported in SAP HANA. Instead, you should use the=operator for assignments.Additionally, there are a few other issues in your query:
The
STUFFfunction is not natively supported in SAP HANA. You need to useSTRING_AGGor a similar function for string concatenation.The
FOR XML PATH('')syntax is specific to SQL Server and won't work in SAP HANA.The
HAVINGclause should be placed correctly within the query structure.Here’s how you can rewrite your query for SAP HANA:
Good Luck!
Tuhin PaulPosted Feb 17, 2025, 8:47 PM
If you are using an older version of SAP HANA that does not support
STRING_AGG, you can use a recursive query to concatenate strings.Tuhin PaulPosted Feb 17, 2025, 8:41 PM
The syntax you are using for
STUFFand string concatenation (FOR XML PATH('')) is specific to SQL Server and is not supported in SAP HANA.SAP HANA does not have a direct equivalent to SQL Server's
STUFForFOR XML PATH(''). Instead, you need to use SAP HANA's string aggregation functions likeSTRING_AGG(introduced in HANA 2 SP04) or other workarounds.STRING_AGGfunction is available starting from SAP HANA 2 SP04 . If you are using an older version of HANA, you will need to use a workaround with recursive queries or user-defined functions (UDFs) to achieve string aggregation.Rijwan AnsariPosted Feb 17, 2025, 12:11 PM
Check this line:
replace with
Daniel WrightPosted Feb 17, 2025, 4:11 AM
The error you are encountering, "Incorrect Syntax near :=", is due to the fact that in SAP HANA SQL, the assignment operator is not ":=", but rather just "=".
Instead of:
It should be:
By making this adjustment and using the correct assignment operator "=", you should be able to resolve the syntax error in your query. Let me know if you need further assistance or clarification!