错误要求的列数少于返回的列数

06odsfpq  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(309)
CREATE OR REPLACE FUNCTION get_user_info ( u_email VARCHAR, u_password VARCHAR, p_name VARCHAR )
RETURNS TABLE(
    user_id             int,                
    given_name          varchar(55),
    family_name         varchar(55),
    password            varchar(255),
    email               varchar(255),
    date_of_birth       date
)
AS $$
BEGIN
    -- If a player display player_name and team_id
    IF p_name != '' THEN
        RETURN QUERY
        SELECT player.user_id, player.given_name, player.family_name, player.password, player.email, player.date_of_birth,
        player.player_name AS player_name, player.team_id AS team_id FROM player
        WHERE player.email = u_email and player.password = u_password;
    -- If not a player then display usual table
    ELSE
        RETURN QUERY
        SELECT * FROM "user"
        WHERE "user".email = u_email and "user".password = u_password;
    END IF;
END $$
LANGUAGE plpgsql;

-- Get user info with arguments - email, password and player_name. If not a player pass '' as player_name
SELECT get_user_info('tomtom@hotmail.com', 'tommy12', 'tomkilla'); -- Player

错误:

ERROR:  structure of query does not match function result type
DETAIL:  Number of returned columns (8) does not match expected column count (6).
CONTEXT:  PL/pgSQL function get_user_info(character varying,character varying,character varying) line 5 at RETURN QUERY
SQL state: 42804

只是想知道为什么我会犯这个错误?我发誓我早就让它工作了,但现在不是了,如果我做一个旁观者,查询就工作了

8mmmxcuj

8mmmxcuj1#

函数只希望返回6列

(
    user_id             int,                
    given_name          varchar(55),
    family_name         varchar(55),
    password            varchar(255),
    email               varchar(255),
    date_of_birth       date
)

但是select语句返回8列

player.user_id, player.given_name, player.family_name, player.password, player.email, player.date_of_birth,player.player_name AS player_name, player.team_id AS team_id

您可以在return语句中添加这两列,如下所示

(
    user_id             int,                
    given_name          varchar(55),
    family_name         varchar(55),
    password            varchar(255),
    email               varchar(255),
    date_of_birth       date,
    player_name         <type>,
    team_id             <type>
)

在else语句中,您将从users表中返回所有列,您可以指定需要返回的列,否则您可能也会遇到问题。

相关问题