0

We replaced an often used sub-select with a scalar function using SELECT INTO. The passed arguments make sure that the resultset always contains only a single integer:

CREATE FUNCTION MAT_AVAIL (DocEntry INT, LineNum INT)
RETURNS VAL INT
LANGUAGE SQLSCRIPT AS
BEGIN
  SELECT
    CASE
      WHEN OWOR."Status" IN ('L', 'C') THEN 3
      WHEN OITW."OnHand" < WOR1."PlannedQty" THEN 2
      WHEN OITW."OnHand" < OITW."IsCommited" THEN 1
      ELSE 0
    END
  INTO VAL
  FROM WOR1
    INNER JOIN OWOR ON OWOR."DocEntry" = WOR1."DocEntry"
    LEFT JOIN OITW ON OITW."ItemCode" = WOR1."ItemCode" AND OITW."WhsCode" = WOR1."wareHouse"
    INNER JOIN OITM ON WOR1."ItemCode" = OITM."ItemCode"
  WHERE WOR1."DocEntry" = :DocEntry AND WOR1."LineNum" = :LineNum;
END;

This is working flawlessly with MSSQL and luckily since a couple of releases also for HANA, but it stays special as its "warning" about the SELECT INTO is actually an exception when trying to execute the function with the DI-API Recordset:

Not recommended feature: Using SELECT INTO in Scalar UDF

So, we have to ask our customers to manually work around this disability, like mentioned here:

alter system alter configuration ('indexserver.ini', 'system') set ('sqlscript', 'enable_select_into_scalar_udf') = 'true' with reconfigure;
alter system alter configuration ('indexserver.ini', 'system') set ('sqlscript', 'sudf_support_level_select_into') = 'silent' with reconfigure;
alter system alter configuration ('indexserver.ini', 'system') set ('sqlscript', 'dynamic_sql_ddl_error_level') = '0' with reconfigure;

We want to get rid of this annoyance, so the question is simple: What is the HANA recommended approach to substitute a common sub-select that returns a scalar value?

NB: While researching, I stumbled over performance discussions with regard to Scalar UDFs with HANA, but even if improving speed with an alternative approach would be welcome, this is not a decisive point here.

5
  • 1
    Can you provide more details on what the scalar function does? Is it a lookup in another table or a computation of some kind? Also, is this guaranteed to return a value or can the result be NULL? Ideally, provide an example code.
    – Lars Br.
    Commented Mar 31, 2022 at 1:30
  • I was looking into table functions, which worked, but would reintroduce sub-selects again and enforce us to apply that to MSSQL as well...
    – boennhoff
    Commented Apr 5, 2022 at 21:05
  • The example code shows that the lookup could return zero records (when no data matching the search criteria is found). This would lead to an SQL error, instead of an empty result set in the calling statement. I would consider modelling this lookup (which it really is) as a view and join this view to the calling statement. In HANA you could even use a parameterised view to force the binding of the DocEntry and LineNo filter conditions.
    – Lars Br.
    Commented Apr 5, 2022 at 22:45
  • Mhmm, are you talking about parameterized calculation views? Any HANA only solution is probably dismissed as we are forced to support MSSQL and HANA interchangeably in the same product, especially for information we need all over the place, a uniform solution is much prefered. So making it a (simple) view might actually be feasible. I'll look into that, thanks!
    – boennhoff
    Commented Apr 6, 2022 at 15:57
  • @LarsBr. You made me doubting about my statement that it always returns a single result, so I checked that first. And the function is only used where the correct conditions are provided either way. An empty resultset would signal corrupt data here, so an SQL error and a follow-up exception is actually good. Now I am unsure how to handle that in a join: INNER JOIN would hide all the other data, LEFT JOIN would hide the corruption...
    – boennhoff
    Commented Apr 7, 2022 at 7:47

1 Answer 1

0

It may be helpful to have a small reproducible example. I have tried the following on my HANA Cloud system without any warning or errors:

CREATE OR REPLACE FUNCTION TEST (inval INT)
RETURNS outval INT
LANGUAGE SQLScript AS
BEGIN
    SELECT :inval INTO outval FROM DUMMY;
END;

SELECT TEST(5) FROM DUMMY;

Does this example capture your issue?

1
  • Hi @mathias-kemeter, I added a simplified example to the question. The actual problem is not the creation of the scalar function but its execution through the DI-API of SAP Business One. I was hoping that since SAP spits out a "warning", that there is a recommend way to do it properly. I dislike having to strive away from sane defaults just to run my code. If the "warning" would actually be a warning and not throw an exception, and therefore enforcing the above mentioned workaround, I would not have asked.
    – boennhoff
    Commented Apr 21, 2022 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.