PostgreSQL:修改过程参数

PostgreSQL:修改过程参数,postgresql,stored-procedures,plpgsql,Postgresql,Stored Procedures,Plpgsql,我有一个数据库表,游戏玩家可以在其中相互评分,并留下可选的评论(如果他们自己有足够好的“声誉”): 这意味着关于:=null的\u上述分配失败 有没有一个好办法让它工作,或者我必须介绍一个临时工。变量在这里 使用PostgreSQL 8.4.7和CentOS Linux 5.5 谢谢大家!! Alex它在PostgreSQL 9.0.2中运行。可能需要升级。8.4中的函数参数隐式地是常量,除非它们是OUT参数。我在8.4文档中找不到这一点,但我确实找到了一些关于从Informix到Postgre

我有一个数据库表,游戏玩家可以在其中相互评分,并留下可选的评论(如果他们自己有足够好的“声誉”):

这意味着关于:=null的\u上述分配失败

有没有一个好办法让它工作,或者我必须介绍一个临时工。变量在这里

使用PostgreSQL 8.4.7和CentOS Linux 5.5

谢谢大家!!
Alex

它在PostgreSQL 9.0.2中运行。可能需要升级。

8.4中的函数参数隐式地是
常量
,除非它们是
OUT
参数。我在8.4文档中找不到这一点,但我确实找到了一些关于从Informix到PostgreSQL的相关讨论:

看起来您只需声明具有相同名称的局部变量即可模拟可变函数参数:

create or replace function pref_update_rep(_id varchar,
        _author varchar, _author_ip inet,
        _good boolean, _fair boolean, _nice boolean,
        _about varchar) returns void as $BODY$
        declare
        rep integer;
        _author varchar := _author;
        begin

以后再看时可能会有点混乱,但这可能比其他选择更可取。

我不太擅长SQL,这是什么意思?id引用了另一个表-pref_用户,id是主键。我认为Catcall注意到,
pref_rep
没有主键,或者,
id
上没有索引。索引引用列是一个好主意,因为当引用表更改时,DB必须检查FKs。
create or replace function pref_update_rep(_id varchar,
        _author varchar, _author_ip inet,
        _good boolean, _fair boolean, _nice boolean,
        _about varchar) returns void as $BODY$
        declare
        rep integer;
        begin

        select
        count(nullif(fair, false)) +
        count(nullif(nice, false)) -
        count(nullif(fair, true)) -
        count(nullif(nice, true))
        into rep from pref_rep where id=_author;

        if (rep <= 0) then
                return;
        end if;

        if (rep < 30) then
                _about := null;
        end if;

        delete from pref_rep
        where id = _id and
        age(last_rated) < interval '1 hour' and
        (author_ip & '255.255.255.0'::inet) =
        (_author_ip & '255.255.255.0'::inet);

        update pref_rep set
            author    = _author,
            author_ip = _author_ip,
            good      = _good,
            fair      = _fair,
            nice      = _nice,
            about     = _about,
            last_rated = current_timestamp
        where id = _id and author = _author;

        if not found then
                insert into pref_rep(id, author, author_ip, good, fair, nice, about)
                values (_id, _author, _author_ip, _good, _fair, _nice, _about);
        end if;
        end;
$BODY$ language plpgsql;
ERROR:  "$7" is declared CONSTANT
CONTEXT:  compilation of PL/pgSQL function "pref_update_rep" near line 21
create or replace function pref_update_rep(_id varchar,
        _author varchar, _author_ip inet,
        _good boolean, _fair boolean, _nice boolean,
        _about varchar) returns void as $BODY$
        declare
        rep integer;
        _author varchar := _author;
        begin