postgresql

Calling procedure with different times as input parameter


I have Postgresql procedure

CREATE OR REPLACE PROCEDURE chk()
  LANGUAGE plpgsql AS
$BODY$
BEGIN

FOR i IN 1 .. 2
LOOP
   call InsrtMockdata();
END LOOP;

END;
$BODY$; 

This procedure runs 2 times and exits

I there any way I can achieve the same by replacing

the range FOR i IN 1 .. 2 as paramaters where I can ran the procedure domically how many time I need


Solution

  • Yes, just add a input parameter as

    CREATE OR REPLACE PROCEDURE chk(times integer)
    LANGUAGE plpgsql AS
    $BODY$
    BEGIN
        FOR i IN 1..times
        LOOP
           call InsrtMockdata();
        END LOOP;
    END;
    $BODY$;
    

    Then call it

    DO $$   
    BEGIN
        call chk(3);
    END $$;