1

I am new to PostgreSQL and have a simple task with a looping structure.

Trying to print 0 to 10. Here is my function:

CREATE OR REPLACE FUNCTION LOOPING()
RETURNS TABLE(asd text) AS
$BODY$declare 
i integer;      
Begin       

i:=0;
WHILE i > 10  LOOP
  select  i;
  i:=(i+1);
END LOOP;

end;$BODY$
LANGUAGE plpgsql IMMUTABLE
COST 100
ROWS 1000;
ALTER FUNCTION LOOPING()
OWNER TO postgres;

I have tried with while loop. If anybody can do this task with for loop it will be very helpful.

4
  • Please do not change the nature of the question after answers have been given. Start a new question for a new question. Commented Oct 29, 2013 at 5:39
  • sorry it was my mistake ... please find new question here stackoverflow.com/questions/19650581/… Commented Oct 29, 2013 at 5:43
  • I rolled back for you - assuming you agree. Commented Oct 29, 2013 at 6:02
  • ya sure.............. Commented Oct 29, 2013 at 6:15

2 Answers 2

2

You have several hick-ups in there. One working way (of many):

CREATE OR REPLACE FUNCTION f_loop()
  RETURNS TABLE(asd int) AS
$BODY$
BEGIN
asd := 0;

WHILE asd < 11 LOOP
   RETURN NEXT;
   asd  := asd + 1;
END LOOP;

END
$BODY$ LANGUAGE plpgsql IMMUTABLE

Call:

SELECT * FROM f_loop();

FOR loop

CREATE OR REPLACE FUNCTION f_loop()
  RETURNS TABLE(asd int) AS
$BODY$
BEGIN

FOR i IN 0..10 LOOP
   asd := i;
   RETURN NEXT;
END LOOP;

END
$BODY$ LANGUAGE plpgsql IMMUTABLE;

Or, for this simple case:

CREATE OR REPLACE FUNCTION f_loop()
  RETURNS SETOF int AS
$BODY$
BEGIN

FOR i IN 0..10 LOOP
   RETURN NEXT i;
END LOOP;

END
$BODY$ LANGUAGE plpgsql IMMUTABLE;

Details in the excellent manual.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much.. can you show me how to do same thing with for loop... please.
what will be a difference when i am dealing with strings in for loops..?
@AijazChauhan: The loop variable i is an integer. You can cast to text. RETURN NEXT i::text;.
1

Your problem as far as I can see is that you have to return the value properly and RETURN NEXT in my experiments (on Pg 9.1) did not work as I expected it to.

I tested this and it was working:

create or replace function loop() returns table (i int)
language plpgsql as
$$
declare i_array int[];
begin
    for i in 0 .. 10 loop
         i_array := i_array || i;
    end loop;
    return query select unnest(i_array);
end;
$$;

4 Comments

you do some really strange. RETURN NEXT works well from 7.4. This is not good advantage :)
Pavel, In looking at Erwin's code, I think the issue is that I usually don't do table returning functions and the problem may be scoping. You can't just return next and have the loop variable returned it seems.
@ChrisTravers: One can't mix RETURN NEXT i with RETURNS TABLE (...) is all. I included another examples to demonstrate both viable routes.
Erwin, yeah, as I noted in your code you copied the variable before you returned it. Hence the comment that this may be a scoping issue. Without copying, you just get a set of 11 NULLs returned... I find that behavior counter-intuitive (which is why I usually use RETURNS SETOF() functions).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.