skip to Main Content

How to create procedure and call/Execute in PostgreSQL

2

Answers


  1. Chosen as BEST ANSWER
    --Create Procedure
    
    Create procedure Proc_StudentLogin(
    
    Sid bigint,
    
    inout get_result refcursor)
    
    Language 'plpgsql'
    
    AS $Body$
    
    Begin
    
    open get_result for 
    
    Select * from Students where id = sid;
    
    End
    
    $Body$;
    
    
    
    --Call procedure
    
    Call Proc_StudentLogin(1,'result');
    
    fetch all in "result";
    

  2. To return a result "table", use a function not a procedure:

    create function Proc_StudentLogin(Sid bigint)
      returns setof students
    as
    $Body$
      select * from students where id = sid;
    $Body$
    language sql
    stable;
    
    -- Use function:
    select * 
    from Proc_StudentLogin(1);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search