我有光标代码:
BEGIN;
DECLARE cliente_cursor
CURSOR FOR SELECT * FROM cliente;
我希望读取表“cliente”的所有内容:
DECLARE cliente_cursor CURSOR
FOR SELECT * FROM cliente
OPEN cliente_cursor
FETCH NEXT FROM cliente_cursor;
While @@FETCH_STATUS=0
BEGIN
FETCH NEXT FROM cliente_cursor;
End
CLOSE cliente_cursor
DEALLOCATE cliente_cursor
我希望您能为PostgreSQL提供可运行的代码。
我一直在寻找解决方案,发现人们通常建议使用函数。我想知道在这个PostgreSQL DBMS中是否有创建类似于SQL Server中代码的方式。
我写过以下代码:
CREATE OR REPLACE FUNCTION MyFunction()
RETURNS setof cliente AS $$
DECLARE
cursor_cliente CURSOR FOR SELECT * FROM cliente;
rec cliente%ROWTYPE;
BEGIN
OPEN cursor_cliente;
loop
--fetch the table row inside the loop
FETCH cursor_cliente INTO rec;
-- check if there is no record
--exit from loop when record not found
if not found then
exit ;
end if;
end loop;
RETURN;
END;
$$ LANGUAGE plpgsql;
但是当我运行它时,我只得到:
select MyFunction();
你有什么想法,应该用什么代码替换它呢?
非常感谢您的帮助!
SELECT * FROM cliente;
有什么不同,可能是因为我不理解 ms-sql-server。 - Jasen