Postgresql 存储过程执行错误

3

我创建了一个存储过程,函数已经成功创建。但是当我调用函数时出现了错误。我该如何解决这个问题?

错误信息如下:

ERROR: 未识别的转换类型说明符“a” CONTEXT: PL/pgSQL 函数 dwgcould.updatescale(integer,integer) 的第6行 EXECUTE 语句 ********** 错误 ********** ERROR: 未识别的转换类型说明符“a” SQL 状态: 22023 上下文:PL/pgSQL 函数 dwgcould.updatescale(integer,integer) 的第6行 EXECUTE 语句

CREATE OR REPLACE FUNCTION scale(IN id integer, IN scale integer) RETURNS integer
AS $$
DECLARE 
    result int;
BEGIN
  IF (SELECT COUNT(*) FROM pg_tables where tablename = format('table_%s_id',id)) > 0 then
    EXECUTE format('update table_%s_id  set geom = ST_Scale(geom, %a, %a',id, scale, scale) using id, scale;
    EXECUTE format('update table_&s_id2  set geom = ST_Scale(geom, %a, %a',id, scale, scale) using id, scale;
    IF FOUND THEN 
        result:= 1;
        return result;
    ELSE 
        result:=0;
        return result;
    END IF; 
  ELSE 
    result:=2;
    return result;
  END IF;
END;
$$ LANGUAGE plpgsql;

你应该使用%s - Patrick
我该如何解决这个问题?不使用“%a”指示符。 - wildplasser
1
ST_Scale(geom,%a,%a) - 没有闭合括号 - Vao Tsun
不相关的,但是:result 变量是无用的。你可以直接使用 return 1; 或者 return 0; - user330315
1个回答

1
您在format()中混淆了位置参数和变量替代的使用方式,并用于EXECUTE命令中:
 EXECUTE format('update table_%s_id set geom = ST_Scale(geom, %s, %s)', id, scale, scale);

如果您想从“EXECUTE”命令返回一个“row_id”,则应在UPDATE查询中明确指定:
CREATE OR REPLACE FUNCTION scale(id integer, scale integer) RETURNS integer AS $$
DECLARE 
    result integer;
BEGIN
  IF (SELECT count(*) FROM pg_tables WHERE tablename = format('table_%s_id',id)) > 0 THEN
    EXECUTE format('UPDATE table_%s_id SET geom = ST_Scale(geom, %s, %s)', id, scale, scale) using id, scale;
    EXECUTE format('UPDATE table_&s_id2 SET geom = ST_Scale(geom, %s, %s)
                    RETURNING row_id',id, scale, scale) INTO result;
    RETURN result;
  END IF;
  RETURN 2;
END;
$$ LANGUAGE plpgsql;

非常感谢,它正在工作。但是当我直接使用查询例如update table_10_10 set geom = ST_Scale(geom, 150, 150)时,更新的行数为346,但结果返回为0。 - Fatih
我没有row_id,所以出现了错误。我只想做一个查询,如果完成则返回1,否则返回0。我认为execute format不起作用。 - Fatih
在你的问题中,你使用了 FOUND。我认为那应该可以工作,但也请参考 https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS - 你也可以使用 GET DIAGNOSTICS integer_var = ROW_COUNT; - Randall

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接