postgresql - 为什么这个 Postgres 函数不正确?

标签 postgresql psql

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 server、oracle 等)相比,这些工具都不合标准。

最佳答案

Why isn't this Postgres function correct?

因为它不包含有效的 SQL。它似乎包含 plpgsql 代码。尝试更改语言:

LANGUAGE plpgsql

关于postgresql - 为什么这个 Postgres 函数不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29952703/

相关文章:

postgresql - 列出 postgresql 表中的所有非空约束

linux - 如何正确观看 psql 命令?

postgresql - 将一个大的 postgres 表拆分为多个 csv

postgresql - "PostGIS Error: type “geography” 不存在”使用 psql 时

sql - PostgreSQL 从一个表中选择全部并从表关系中加入计数

node.js - 如何检测和记录我的 KnexJS 交易?

sql - 如何通过一个请求计算玩家的多个排名?

python - Flask 自动重新连接 PostgreSQL

python - 处理 sqlalchemy 断开连接的更好方法

postgresql - postgresql中多列上的多个索引与单个索引