简体   繁体   English

PostgreSQL字符串连接:错误:“ msg”或附近的语法错误

[英]Postgresql string concatenation: error: syntax error at or near “msg”

I am trying to raise an issue in a Postgresql function using a concatenated message, but I get an error message at runtime: 我正在尝试使用级联消息在Postgresql函数中引发问题,但在运行时出现错误消息:

error: syntax error at or near "msg"

The code is the following: 代码如下:

CREATE OR REPLACE FUNCTION insertUserAccount(
    id             bigint,
    nname          varchar(40),
    email          varchar(40),
    pwd            varchar,
    status         smallint,
    last_update  bigint,
    preferences    json,
    bits           integer,
    adm            json)
    RETURNS bigint AS $$
DECLARE
    rowc INTEGER;
    ret_id bigint;
    msg text;
BEGIN
    ...
    IF ( rowc > 0 ) THEN
        msg = 'User account already exists or name or email is unavailable (id=' || id
            || ', name=' || nname
            || ', email=' || email || ')';
        RAISE EXCEPTION msg USING ERRCODE = '23505';
    ELSE
    ...

The actual problem is the faulty syntax for RAISE EXCEPTION . 实际的问题是RAISE EXCEPTION语法错误。 I would simplify overall: 我将整体简化:

IF rowc > 0 THEN
   RAISE EXCEPTION 'User account already exists or name or email is unavailable (id=%,name=%,email=%)'
                 , id, nname, email  USING ERRCODE = '23505';
ELSE ...

The variable msg might not be needed at all then. 那时可能根本不需要变量msg It is generally best to keep the number of assignments low, since those are rather expensive in PL/pgSQL (as compared to other programming languages). 通常最好保持较低的分配数量,因为在PL / pgSQL中,分配数量相当昂贵(与其他编程语言相比)。 Not dramatic, but still .. 不太戏剧性,但仍然..

使用:=在PL / pgSQL中分配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM