@@Fetch_status in postgreSQL

Deadly 提交于 2020-01-15 12:11:13

问题


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?


回答1:


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 ];



回答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.



来源:https://stackoverflow.com/questions/30605982/fetch-status-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!