为什么这个 Postgres 函数不正确?
Why isn't this Postgres function correct?
CREATE OR REPLACE FUNCTION public.create_user(_name TEXT, _pass TEXT, _iconid INTEGER)
RETURNS user_info
LANGUAGE sql
AS $$
DECLARE
_newid INTEGER;
BEGIN
INSERT INTO users(name, password, iconid) VALUES (_name, crypt(_pass, gen_salt('bf')), _iconid);
SELECT _newid = currval(pg_get_serial_sequence('users', 'id'));
SELECT u.id, u.name, u.rating, t.name, i.path, i.name FROM users u
LEFT OUTER JOIN usertypes t ON t.id = u.typeid
LEFT OUTER JOIN usericons i ON i.id = u.iconid
WHERE u.id = _newid;
END
$$;
我得到:
ERROR: syntax error at or near "INTEGER"
Line 6: _newid INTEGER;
^
我确定我遗漏了一些东西,但到目前为止我真的很沮丧,因为与几乎所有其他主要数据库相比,这些工具都不合标准 - mysql、sql 服务器、oracle 等.
Why isn't this Postgres function correct?
因为它不包含有效的 SQL。它似乎包含 plpgsql 代码。尝试更改语言:
LANGUAGE plpgsql
CREATE OR REPLACE FUNCTION public.create_user(_name TEXT, _pass TEXT, _iconid INTEGER)
RETURNS user_info
LANGUAGE sql
AS $$
DECLARE
_newid INTEGER;
BEGIN
INSERT INTO users(name, password, iconid) VALUES (_name, crypt(_pass, gen_salt('bf')), _iconid);
SELECT _newid = currval(pg_get_serial_sequence('users', 'id'));
SELECT u.id, u.name, u.rating, t.name, i.path, i.name FROM users u
LEFT OUTER JOIN usertypes t ON t.id = u.typeid
LEFT OUTER JOIN usericons i ON i.id = u.iconid
WHERE u.id = _newid;
END
$$;
我得到:
ERROR: syntax error at or near "INTEGER"
Line 6: _newid INTEGER;
^
我确定我遗漏了一些东西,但到目前为止我真的很沮丧,因为与几乎所有其他主要数据库相比,这些工具都不合标准 - mysql、sql 服务器、oracle 等.
Why isn't this Postgres function correct?
因为它不包含有效的 SQL。它似乎包含 plpgsql 代码。尝试更改语言:
LANGUAGE plpgsql