FOR loop Netezza issue

2019-08-08 04:40发布

问题:

I'm working with stored procedures in netezza. I want to loop over a range of values. The upper bound on the loop is passed as a variable into the sproc by the user.

i.e. EXECUTE SPROC(12);

so problem is that Netezza (aginity workbench) won't accept this input variable as the upper bound on the loop.

i.e.

DECLARE 
 x alias as $1.
begin 
   for i in 1..x loop
     ...do stufff... 
    end loop; 
end;

I know that this can be solved using loop and exit style loop but It's eating me up as to why i can't do the above given that the documentation suggests that it's possible to do so.

Anyone know why this doesn't work or how to make it work?

Thanks. Clancy.

回答1:

Please find below working example -

CREATE OR REPLACE PROCEDURE generateTime(integer)
LANGUAGE NZPLSQL RETURNS varchar(255) AS

BEGIN_PROC

DECLARE
        p_abc     integer;
        p_bcd     integer;

        p_var1    ALIAS FOR $1;


BEGIN
        p_bcd := ISNULL(p_var1, 10);

        raise notice 'p_bcd=%',p_bcd;

        FOR p_abc in 0..(p_bcd)
        LOOP
                raise notice 'Hello World %', p_abc;
        END LOOP;
END;


END_PROC;

Hope this will help.