Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

node.js - How to call a postgresql function that uses refcursors with node-postgres?

I have a function that returns multiple result sets using refcursor. To give you an idea, here is how it is done:

   CREATE OR REPLACE FUNCTION showcars(ref1 refcursor, ref2 refcursor) RETURNS SETOF refcursor AS $$                        
    BEGIN
      OPEN ref1 FOR SELECT model FROM cars WHERE manufacturer = 'Ford';   -- Open the first cursor
      RETURN NEXT ref1;                                                                              -- Return the cursor to the caller
 
      OPEN ref2 FOR SELECT sold FROM sales WHERE manufacturer = 'Ford';   -- Open the second cursor
      RETURN NEXT ref2;                                                                              -- Return the cursor to the caller
    END;
    $$ LANGUAGE plpgsql;

I call the above function like this to get the multiple results back:

BEGIN;

SELECT showcars(_ref1 := 'CarModels', _ref2 := 'CarSales');

FETCH ALL IN "CarModels";
FETCH ALL IN "CarSales";
COMMIT;

As you can see I pass the cursor name as a parameter, so the caller always knows which cursor to fetch. How do I run this from node-postgres to get the results back?

question from:https://stackoverflow.com/questions/65874849/how-to-call-a-postgresql-function-that-uses-refcursors-with-node-postgres

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...