I am trying to use a variable as input for a WHERE
condition having an IN
clause using SAP HANA.
For example:
DO BEGIN DECLARE a VARCHAR(100) = '1000,1001'; DECLARE b NVARCHAR(100) ARRAY = ARRAY('1000','1001'); DECLARE c TABLE (valuelist NVARCHAR(100)); --SELECT '1001' INTO valuelist FROM :c; --:c.INSERT('1001','1000'); DECLARE e NVARCHAR(100) = ' ''1000'',''1001'' '; DECLARE f NVARCHAR(100) = '"STATIONID" IN (''1000'',''1001'')'; select STATIONID from MY_TABLE where STATIONID IN ('1000','1001') -- working fine, but want to use a variable here -- where STATIONID IN (:a) -- where STATIONID IN (:b) -- where STATIONID IN :c -- where :f order by STATIONID; END
However, all my attempts, you can see some of them above, do fail when having multiple values in my condition. STATIONID
really is a string field.
Any hints on how to do this?
Advertisement
Answer
Finally I figured out how I can use a variable array
together with a variable table
by using UNNEST
. This is working just fine:
DO BEGIN DECLARE STATION_TMP_ARRAY NVARCHAR(10) ARRAY = ARRAY('1000','1001','1002','1003','1004'); DECLARE STATION_TMP_TABLE TABLE ("tmp_stationid" NVARCHAR(10)) = UNNEST(:STATION_TMP_ARRAY) AS ("tmp_stationid"); select STATIONID from MY_TABLE where "STATIONID" IN (SELECT "tmp_stationid" FROM :STATION_TMP_TABLE) order by STATIONID; END