Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL cursor with arguments

As the documentation describes here, I need to declare a cursor that accepts arguments at OPEN time.

My query looks something similar to:

DECLARE cur CURSOR (argName character varying) FOR SELECT * FROM "TableName" WHERE "SomeColumn" = argName;

When I do this, I get a lovely error:

ERROR:  syntax error at or near "("
LINE 1: DECLARE cur CURSOR (argName character varying) FOR SELECT * FROM...
                           ^

It seems that PostgreSQL is not accepting this form of cursor declaration. Is there any way to solve this? Any workaround?

like image 884
gcontreras Avatar asked Oct 26 '25 16:10

gcontreras


2 Answers

That syntax is only valid inside plpgsql functions.

http://www.postgresql.org/docs/current/static/plpgsql-cursors.html

like image 135
Clodoaldo Neto Avatar answered Oct 28 '25 06:10

Clodoaldo Neto


Try something like:

DECLARE 
  argName varchar;
  cur CURSOR FOR SELECT * FROM "TableName" WHERE "SomeColumn" = argName;

The argName will be taken into the query when you OPEN this cursor.

like image 42
Ihor Romanchenko Avatar answered Oct 28 '25 06:10

Ihor Romanchenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!