Search code examples
sqlstored-procedureshana

Conditional WHERE-clause at stored procedures


I want to apply an additional WHERE-Parameter, if a specific value is given. We're currently working on SAP HANA, but we may are able to adapt strict stored-procedure programming to this platform. any help is very appreciated! So here's the Code:

PROCEDURE test (
   IN id integer,
   IN testVal VARCHAR(20) DEFAULT '*',
   out ex_return DB-SCHEME     
)
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER
--DEFAULT SCHEMA <default_schema_name>
READS SQL DATA AS
BEGIN

ex_return =
    SELECT  
        L."ID",
        LW."ID"
    FROM DB1 L
        INNER JOIN DB2 LW
            ON L."id" = LW."id"
    WHERE 
            L."id" = :id
        AND LW."testVal" LIKE :testVal       -- this one only, if :testVal != '*' 
;

END

What have I tried yet? I tried to CONCAT the SELECT with a calculated WHERE (IF-Conditions) an then the EXECUTE-Command, but it seems like SAP HANA doesn't support that. Then I tried to match the requirements with CASE within the WHERE-Clause:

WHERE ... CASE :wert <> '*' THEN ... END

Solution

  • AND (LW."testVal" LIKE :testVal OR :testVal = '*')