我是PostgreSQL的新手。我正在尝试将SQL store procedure
转换为postgreSQL store function.
,就像“成功创建以下存储函数一样”,但是当我尝试执行时,出现以下错误消息。我找不到需要替换PERFORM
的地方。
ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
SQL存储过程
CREATE PROCEDURE [dbo].[roomType]
@intInstId int
AS
BEGIN
SELECT
nClientRoomTypeID,sClientRTDesc,sClientRTName,sClientRTCode
FROM
ClientRoomType
WHERE
ClientRoomType.nInstID=@intInstId
ORDER BY
ClientRoomType.sClientRTCode
END
GO
PostgreSQl存储功能
CREATE OR REPLACE FUNCTION roomType (int_inst_id int)
RETURNS VOID
AS $$
BEGIN
SELECT
nclient_room_type_id,sclient_rt_desc,sclient_rt_name,sclient_rt_code
FROM
clientroomtype
WHERE
clientroomtype.ninst_id=int_inst_id
ORDER BY
clientroomtype.sclient_rt_code;
END;
$$ LANGUAGE plpgsql;
答案 0 :(得分:0)
我假设您正在尝试使用函数返回查询结果。实际上,您实际上不需要plpgsql
语言,但是如果需要其他语言,请使用以下语法:
CREATE OR REPLACE FUNCTION roomType (int_inst_id int)
RETURNS TABLE (res_nclient_room_type_id INT,res_sclient_rt_desc TEXT,res_sclient_rt_name TEXT, res_sclient_rt_code TEXT)
AS $$
BEGIN
RETURN QUERY
SELECT
nclient_room_type_id,sclient_rt_desc,sclient_rt_name,sclient_rt_code
FROM
clientroomtype
WHERE
clientroomtype.ninst_id=int_inst_id
ORDER BY
clientroomtype.sclient_rt_code;
END;
$$ LANGUAGE plpgsql;
如何使用它?
SELECT * FROM roomType(1);
由于我没有您的数据,因此无法测试。但它遵循以下原则:
CREATE OR REPLACE FUNCTION f ()
RETURNS TABLE (b boolean, x int)
AS $$
BEGIN
RETURN QUERY SELECT TRUE, 1;
END;
$$ LANGUAGE plpgsql;
SELECT * FROM f();
b | x
---+---
t | 1
(1 Zeile)