最近看PG源码,在语法分析中有看到CURRENT OF元素。在PG游标中,WHERE CURRENT OF可以用来使用游标更新或删除当前指向的行。
语法:UPDATE table SET ... WHERE CURRENT OF cursor;
语法:DELETE FROM table WHERE CURRENT OF cursor;
实例:
select * from tf1;
c1 | c2 | c3 | c4 | c5
----+------+--------+----------+-------
1 | 1000 | China | Dalian | 23000
2 | 4000 | Janpan | Tokio | 45000
3 | 1500 | China | Xian | 25000
4 | 300 | China | Changsha | 24000
5 | 400 | USA | New York | 35000
6 | 5000 | USA | Bostom | 15000
CREATE OR REPLACE FUNCTION tfun3() RETURNS int AS $$
DECLARE
curs1 refcursor;
y tf1%ROWTYPE;
BEGIN
open curs1 FOR SELECT * FROM tf1;
fetch last from curs1 into y;
RAISE NOTICE 'curs1 : %', y.c2;
delete from tf1 WHERE CURRENT OF curs1;
return 0;
END;
$$ LANGUAGE plpgsql;
select tfun3();
select * from tf1;
c1 | c2 | c3 | c4 | c5
----+------+--------+----------+-------
1 | 1000 | China | Dalian | 23000
2 | 4000 | Janpan | Tokio | 45000
3 | 1500 | China | Xian | 25000
4 | 300 | China | Changsha | 24000
5 | 400 | USA | New York | 35000
这样,最后一行就被删除。