Hi I have this function:
CREATE or replace FUNCTION Tablereturn (SWITCHER INTEGER)
RETURNS TABLE (Test CHAR(9),
tester INTEGER
)
LANGUAGE SQL
CONTAINS SQL
DETERMINISTIC
NO EXTERNAL ACTION
BEGIN
DECLARE SELECT1, SELECT2 VARCHAR(1024);
set select1 ='SELECT TEST, TESTER FROM TESTTAB';
set select2 ='SELECT DUMMY, JAR, BRAND FROM TESTTAB';
IF (SWITCHER = '1') THEN return select1;
ELSEIF (SWITCHER = '2') THEN return select2;
END IF;
RETURN TABLE;
END@
Calling would be
select TEST from TABLE(Tablereturn(1))@
or
select JAR from TABLE(Tablereturn(2))@
The problem is, that it doesn't work. Compiler says that after return the unexpected Token "SELECT1" is there. I want to be able to call it as a table and select values as I need them from the call. I can't just call it as a procedure with the select as return, since I need to work with the select as a table and change the returned output in a bigger select. Can I have EXECUTE IMMEDIATE in a function?
Any ideas? The other question is, how can I make the function return different tables? The select2 returns 3 values while select1 returns only 2.
Thank you for your help.
RETURN statement must be the last statement of the function. There is a class of functions called 'pipelined' where you can use 'if then else' logic. Like this:
An alternative to using
PIPE
would be something simple like this