So I am trying to create a procedure with the following logic.
- Split a delimited string into a (temp) table
- Iterate the above table to perform an action with each row value.
The Split function I have is thus :
CREATE PROCEDURE "myschema"."SPLIT_TEST"(TEXT nvarchar(4000))
AS
BEGIN
declare _items nvarchar(100) ARRAY;
declare _text nvarchar(100);
declare _index integer;
_text := :TEXT;
_index := 1;
WHILE LOCATE(:_text,',') > 0 DO
_items[:_index] := SUBSTR_BEFORE(:_text,',');
_text := SUBSTR_AFTER(:_text,',');
_index := :_index + 1;
END WHILE;
_items[:_index] := :_text;
rst = UNNEST(:_items) AS ("items");
SELECT * FROM :rst;
END;
Now this works fine to call by itself : CALL "myschema"."SPLIT_TEST" ('a,c,e,g')
But I am struggling on how to call this from my main procedure and iterate the results.
I need to insert the results into a temp table; or have SPLIT_TEST return a table object.
If using psuedo code, would be something like :
CREATE LOCAL TEMPORARY TABLE #temp (
"txt" nvarchar(128)
);
insert into #temp("txt")
CALL "myschema"."SPLIT_TEST" ('a,c,e,g');
<iterate #temp>
I hope someone can guide me in the right direction !?!
Many Thanks