如何 return 来自 postgresql 函数的查询结果

How to return a query result from postgresql function

我是 Postgres 的新手,在 ms sql 服务器中我们可以编写存储过程来执行逻辑和 return 一个 select 语句。同样,我用 return 类型 table 编写了以下 pqsql 函数。但它显示了一些不正确的语法错误,但如果我用整数替换 return 类型并注释掉 select 语句它工作正常。

以下是 return 类型 table

的函数
CREATE OR REPLACE FUNCTION candidate_save
(
    p_name varchar(50),
    p_dob date,
    p_course_level_code integer,
    p_email varchar(50),
    p_mob character(10),
    p_sslc_regno varchar(10),
    p_sslc_year_of_passing character(4),
    p_sslc_board_of_examination  integer,
    p_password character(128),
    p_ip_address varchar(15)
)
RETURNS TABLE 
(
    regno char(10),
    name varchar(50),
    dob date,
    course_level_code integer,
    email varchar(50),
    mob character(10),
    sslc_regno varchar(10),
    sslc_year_of_passing character(4),
    sslc_boe  integer
)
AS 
$BODY$
DECLARE
    max_row_count integer;
    random_no integer;
    v_regno character(10);
BEGIN

    SELECT COUNT(*) INTO max_row_count FROM candidates WHERE course_level_code = p_course_level_code;

    max_row_count := max_row_count + 1;

    v_regno := cast(p_course_level_code as character) || cast( trunc(random() * 89999 + 10000) as integer) || to_char(max_row_count, 'FM0000');

    INSERT INTO candidates 
        (regno, name, dob, course_level_code, email, mob, sslc_regno, sslc_year_of_passing, sslc_board_of_examination, password, created_on, ip_address)
    VALUES 
        (v_regno, p_name, p_dob, p_course_level_code, p_email, p_mob, p_sslc_regno, p_sslc_year_of_passing, p_sslc_board_of_examination, p_password, now(), p_ip_address);

    RETURN QUERY
        SELECT regno, name, dob, course_level_name, email, mob, sslc_regno, sslc_year_of_passing, c.board as sslc_boe 
        FROM candidates a 
            INNER JOIN course_levels b on a.course_level_code = b.course_level_code
            INNER JOIN sslc_board_of_examinations c ON a.sslc_board_of_examination = boe_code
        WHERE regno = v_regno;

END;
$BODY$
LANGUAGE plpgsql;

创建上述函数时显示以下错误

ERROR:  syntax error at or near ""
LINE 1: INSERT INTO candidates (  ,   ,   ,   ,   ,   , ...
                                 ^
QUERY:  INSERT INTO candidates (  ,   ,   ,   ,   ,   ,   ,   , sslc_board_of_examination, password, created_on, ip_address) VALUES (  ,   ,   ,   ,   ,   ,   ,   ,   ,   , now(),   )
CONTEXT:  SQL statement in PL/PgSQL function "candidate_save" near line 30

********** Error **********

ERROR: syntax error at or near ""
SQL state: 42601
Context: SQL statement in PL/PgSQL function "candidate_save" near line 30

但是如果我用整数替换 table 它工作正常。

CREATE OR REPLACE FUNCTION candidate_save
(
    p_name varchar(50),
    p_dob date,
    p_course_level_code integer,
    p_email varchar(50),
    p_mob character(10),
    p_sslc_regno varchar(10),
    p_sslc_year_of_passing character(4),
    p_sslc_board_of_examination  integer,
    p_password character(128),
    p_ip_address varchar(15)
)
RETURNS integer
AS 
$BODY$
DECLARE
    max_row_count integer;
    random_no integer;
    v_regno character(10);
BEGIN

    SELECT COUNT(*) INTO max_row_count FROM candidates WHERE course_level_code = p_course_level_code;

    max_row_count := max_row_count + 1;

    v_regno := cast(p_course_level_code as character) || cast( trunc(random() * 89999 + 10000) as integer) || to_char(max_row_count, 'FM0000');

    INSERT INTO candidates 
        (regno, name, dob, course_level_code, email, mob, sslc_regno, sslc_year_of_passing, sslc_board_of_examination, password, created_on, ip_address)
    VALUES 
        (v_regno, p_name, p_dob, p_course_level_code, p_email, p_mob, p_sslc_regno, p_sslc_year_of_passing, p_sslc_board_of_examination, p_password, now(), p_ip_address);

    RETURN 1;

END;
$BODY$
LANGUAGE plpgsql;

如果我做错了什么请。让我知道。

我正在使用 Postgres 9.4

INSERT INTO candidates 
    (regno
     ^------- The error

ERROR:  syntax error at or near ""
LINE 1: INSERT INTO candidates ( 

问题是 regno 是一个 plpgsql 变量,因为它是输出 table.

中的列名
RETURNS TABLE 
(
    regno char(10),

RETURNS TABLE 中的每个条目都注册为变量,因此您可以分配给它并使用 RETURN NEXT

解析和处理时,语句中的 plpgsql 变量被替换为位置参数,如 </code>、<code> 等。这就是错误的原因,也是为什么某些错误insert column-name-list 中后面的条目不会被替换。它们不会与参数名称冲突。

使用不同的变量名。

我认为 9.5 检测参数名称冲突并报告更清晰的错误。

在我更改 return table 中的变量名称后,它终于开始工作了。 感谢大家