Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql stored procedure execute error

Tags:

postgresql

I created a stored procedure. The function is created successfully. When I call function I get an error. How can I solve this problem?

The error is

ERROR: unrecognized conversion type specifier "a" CONTEXT: PL/pgSQL function dwgcould.updatescale(integer,integer) line 6 at EXECUTE statement ********** Error ********** ERROR: unrecognized conversion type specifier "a" SQL state: 22023 Context: PL/pgSQL function dwgcould.updatescale(integer,integer) line 6 at EXECUTE statement

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;
like image 795
Fatih Avatar asked Oct 23 '25 18:10

Fatih


1 Answers

You mix up the use of positional parameters in format() and variables for substitution in the EXECUTE command:

 EXECUTE format('update table_%s_id set geom = ST_Scale(geom, %s, %s)', id, scale, scale);

If you want to return a row_idfrom the `EXECUTE command, the you should explicitly specify that in the UPDATE query:

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;
like image 157
Patrick Avatar answered Oct 26 '25 07:10

Patrick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!