Home > database >  plpgsql $[position] is relative?
plpgsql $[position] is relative?

Time:05-17

create or replace function exc_using(int, text) returns int as
    $$
        declare i int;
        begin
        for i in execute 'select * from generate_series(1,$1) ' using $1 1 loop
            raise notice '% ', i;
            raise notice 'i   i *3: % ', i   i * 3;
        end loop;
        execute 'select $2   $2 * 3   length($1) '
            into i using $2, $1;
        return i;
        end
    $$
language plpgsql;

seems A

    execute 'select $2   $2 * 3   length($1) '
        into i using $2, $1;
    return i;

is the same as: B

    execute 'select $1   $1 * 3   length($2) '
        into i using $1, $2;
    return i;

My preception is that $2 Absolutely refer to function second argument that data type is text. but seems plpgsql $[position] is relative. But I cannot get manual reference for this edge case.

CodePudding user response:

Of course they are relative. What else would they be? But what are they relative to?

Inside the EXECUTE string, the $1 and $2 are relative to the USING. Outside, they are relative to the function parameters.

  • Related