【问题标题】:Error Message : Query has no destination for result data错误消息:查询没有结果数据的目的地
【发布时间】:2018-07-20 11:40:10
【问题描述】:

我是 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;

【问题讨论】:

    标签: postgresql stored-procedures plpgsql stored-functions


    【解决方案1】:

    我假设您正在尝试使用您的函数返回查询结果。您实际上不需要 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)
    

    【讨论】:

    • 我试过这个语法我得到这个错误ERROR: column reference "nclient_room_type_id" is ambiguous LINE 2: nclient_room_type_id,sclient_rt_desc,sclient_rt_name,sclie... ^ DETAIL: It could refer to either a PL/pgSQL variable or a table column
    • 是的,它的工作,但结果得到这种格式(1,"TEMPORARILY NOT ASSIGNED","TEMPORARILY NOT ASSIGNED",000)为什么列不显示
    • 我会再次更新我的问题并提示 :-) 试试看:select * from roomType (1);
    • 我添加了结果截图
    猜你喜欢
    • 2014-07-27
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多