Skip to Content

Scalar-valued function with dynamic column selection

Hello,

I would like to do something like this in a scalar-valued function (hdbscalarfunction) oin HANA in order to get back the average value of a key figure with respect to a certain characteristic:

FUNCTION "JPCHURN"."ml.jpchurn.functions::getAveFeature" ( in_clsID Integer , in_featureName nvarchar(256) ) 
	RETURNS ex_aveValue Double
	LANGUAGE SQLSCRIPT
	SQL SECURITY INVOKER AS
BEGIN
/***************************** 
	Write your function logic
 *****************************/
declare lv_String nvarchar(256);
lv_String = 'a.' || :in_featureName;

 select (:lv_String) into ex_aveValue
 from "JPCHURN"."ml.jpchurn.data::jpChurn.tab_DataRaw" as a 
 inner join "JPCHURN"."CustChurnAPLModelsvClust_01_resultTrain" as b
 on a."AccountID" = b."AccountID"
 where b.closest_cluster_1 = :in_clsID;

However the dynamic select for :lv_string which actually contains the name of the field does not work. I could do it as a workaround with an if-else branch and then typing in the names of the fields manually. However beside this is only pseudo-dynamic I have ~ 25 fields which could also change.

Is there any idea how to achieve this? Dynamic SQL is btw not allowed in scalar-valued functions.

Thanks + Regards, Ingo

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Posted on Oct 14, 2019 at 03:07 AM
    -1

    You can use dynamic SQL in HANA Procedure object. Refer below link to refer an example:

    https://help.sap.com/viewer/de2486ee947e43e684d39702027f8a94/2.0.02/en-US/093c4fd307064f838cb582555c187b9e.html

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.