Skip to main content

How to Create And Excute Function With Refcursor in Postgresql



How to Create And Execute Function With Refcursor in Postgresql



Create Two Tables

create table EmployeeDetails(EmployeeId serial primary key,FirstName varchar(60), LastName varchar(60),Place varchar(100),Country varchar(40),PhoneNo varchar(40),password varchar(100)); create table RoomDetails(RoomId serial primary key ,RoomName varchar(100),RoomSize varchar(20))
Insert Values

Insert into RoomDetails(RoomName,RoomSize) values('Abdul kalam','10'), ('aryabhata','15'), ('Nasa','20'), ('vivekananda','10'), ('Rohini','5'); insert into EmployeeDetails(FirstName,LastName,Place,Country,PhoneNo,password) values('Vasanth', 'R','SGRI', 'India','8056512886','hello'), ('Ram', 'S', 'SGRI', 'India', '9366180976','hello'), ('Ravi', 'E', 'SGRI','India', '123456789','hello'), ('Krishna','R', 'SGRI', 'India', '987654321','hello'), ('Divya', 'R', 'SGRI', 'India', '7502036282','hello');

Create a Required Function

  CREATE OR REPLACE FUNCTION show_multiple(ref1 refcursor,ref2 refcursor) RETURNS SETOF refcursor AS $$                          
    BEGIN
      OPEN ref1 FOR SELECT * FROM EmployeeDetails;   
      RETURN NEXT ref1;                              -- Return the cursor to the caller

      OPEN ref2 FOR SELECT * FROM RoomDetails;  
      RETURN NEXT ref2;                            -- Return the cursor to the caller
    END;
    $$ LANGUAGE plpgsql;
Then Unchceck Auto commit







Run this one
select * from show_multiple('ref1','ref2');
Then this
FETCH ALL IN "ref1";
Finally this
FETCH ALL IN "ref2";
If you want to close transaction use commit
commit;
Then Again check the auto commit for normal transactions

Comments

Popular posts from this blog

Push Notification Device To Device (Android studio + Firebase)

Push Notification Device To Device (Android studio + Firebase) Git Link Tasks Done Trigger Notification within the android device Send Notification to Another Device Daily Notification Using AlarmManager Trigger Notification within the android device Add Dependencies dependencies { ... compile "com.android.support:support-v4:24.1.1" } Add a  button < Button    android :id= "@+id/notification_Self"    android :layout_width= "wrap_content"    android :layout_height= "wrap_content"    android :layout_centerInParent= "true"    android :layout_marginStart= "8dp"    android :layout_marginTop= "8dp"    android :layout_marginEnd= "8dp"    android :layout_marginBottom= "8dp"    android :text= "Self Notification"    app :layout_constraintBottom_toBottomOf= "parent"    app :layout_constraintEnd_t...