PostgreSQL sql-state: 42601 on SELECT INTO

落爺英雄遲暮 提交于 2019-12-11 16:47:45

问题


Well, i'm trying to create a simple procedure, that check if user with such login, and if no - adding new row in users table. But stuck with unexpected problem.

CREATE OR REPLACE FUNCTION register_user(character varying, character varying, character varying,character varying,character varying)
  RETURNS bigint AS
$BODY$
DECLARE
    new_user_login ALIAS FOR $1;
    new_user_password ALIAS FOR $2;
    new_user_email ALIAS FOR $3;
    new_user_first_name ALIAS FOR $4;
    new_user_last_name ALIAS FOR $5;
    login_exist bigint;
    new_user_id bigint;
    emails_array character varying array; --yep, it's array of emails
BEGIN       
    SELECT INTO login_exist count(login) FROM users WHERE users.login = new_user_login;
    IF (login_exist = 0) THEN
        SELECT array_append(emails_array, new_user_email);
        INSERT INTO users (login,password,emails,first_name,last_name) 
        VALUES (new_user_login,new_user_password,emails_array,new_user_first_name,new_user_last_name)
        RETURNING id INTO new_user_id;
        RETURN new_user_id;
    ELSE
        RETURN 0;
    END IF;
END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

It returns sql-state: 42601 on SELECT INTO. But if only count is 0. When login is exist it correctly return 0; What the problem is? I'm even have no idea what is this. thx for help;


回答1:


This instruction:

SELECT array_append(emails_array, new_user_email);

should fail because array_append returns the modified array and ignoring the result of the select is not allowed.

If you wanted to append into the source array, this should be:

SELECT array_append(emails_array, new_user_email) INTO emails_array;

However this is not even necessary. You may simplify your function body into:

BEGIN

INSERT INTO users (login,password,emails,first_name,last_name) 
SELECT new_user_login,new_user_password,array[new_user_email],new_user_first_name,new_user_last_name
WHERE NOT EXISTS (select 1 FROM users WHERE users.login = new_user_login)
RETURNING id INTO new_user_id;

RETURN coalesce(new_user_id,0);

END;



回答2:


In addition to what @Daniel already answered, don't use the outdated ALIAS for naming function parameters. Quoting the manual here:

It's best to use it only for the purpose of overriding predetermined names.

Use argument names instead:

CREATE OR REPLACE FUNCTION register_user(_login      text
                                       , _password   text
                                       , _email      text
                                       , _first_name text
                                       , _last_name  text)
  RETURNS bigint AS
$func$
DECLARE
   _id  bigint;
BEGIN
   INSERT INTO users (login, password, emails, first_name, last_name) 
   SELECT _login, _password, ARRAY[_email], _first_name, _last_name
   WHERE  NOT EXISTS (SELECT 1 FROM users WHERE login = _login)
   RETURNING id INTO _id;

   RETURN COALESCE(_id, 0);
END
$func$  LANGUAGE plpgsql;


来源:https://stackoverflow.com/questions/20282639/postgresql-sql-state-42601-on-select-into

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!