SuperM4n
SuperM4n

Reputation: 203

@@Fetch_status in postgreSQL

I'm transfering my database from MS SQL Server to PostgreSQL and I have a problem with this trigger:

CREATE TRIGGER added_clients ON client
FOR INSERT
AS
BEGIN
DECLARE cursor_inserted CURSOR
FOR SELECT name, surname FROM inserted;
OPEN cursor_inserted
DECLARE @name varchar(15), @surname varchar(20)
FETCH NEXT FROM cursor_inserted INTO @name, @surname
WHILE @@FETCH_STATUS = 0
BEGIN
print 'Added: '+@name+' '+ @surname
FETCH NEXT FROM cursor_inserted INTO @name, @surname
END
CLOSE cursor_inserted
DEALLOCATE cursor_inserted
END
GO

What is the equivalent of @@FETCH_STATUS in postgreSQL?

Upvotes: 2

Views: 3648

Answers (2)

BrianAtkins
BrianAtkins

Reputation: 1349

Here is the Cursor documentation for PostgreSQL

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

Looping through a cursor:

[ <<label>> ]
FOR recordvar IN bound_cursorvar [ ( [ argument_name := ] argument_value [, ...] ) ] LOOP
    statements
END LOOP [ label ];

Upvotes: 2

The equivalent of @@fetch_status in PostgreSQL is FOUND.

You will find more information here: https://www.postgresql.org/docs/9.1/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS

In your trigger, you only need to write WHILE (FOUND) instead of @@fetch_status = 0.

I hope you find useful my answer.

Upvotes: 3

Related Questions