简体   繁体   中英

unable to create dynamic table using function with execute format option in postgresql

i am trying to create dynamic table using function however i am getting error.

my code as below.

create or replace function fn_while_loop_upd_table(val int) returns BOOLEAN as 

$$
DECLARE lv_count int = 0;
        lv_in int = 100;
        mybool BOOLEAN = false;
        t1 varchar(30) = val;
BEGIN
        execute format('drop table if exists t_tab%%t1');
        EXECUTE format('create table if not exists t_tab %%t1(myval int)');
        while lv_count <= VAL
                loop 
                lv_in = lv_in + 1;
                insert into t_tab values(lv_in);
                lv_count = lv_count + 1;
                end loop;
        RETURN mybool;
end;

$$
LANGUAGE plpgsql;

note i have tried with single % also error as below.

ERROR:  syntax error at or near "%"
LINE 1: create table if not exists t_tab %t1(myval int)
                                         ^
QUERY:  create table if not exists t_tab %t1(myval int)
CONTEXT:  PL/pgSQL function fn_while_loop_upd_table(integer) line 8 at EXECUTE
SQL state: 42601
create or replace function fn_while_loop_upd_table(val int) returns BOOLEAN as 

$$
DECLARE lv_count int = 0;
        lv_in int = 100;
        mybool BOOLEAN = false;
        t1 varchar(30) = val;
BEGIN
        execute format('drop table if exists t_tab%%t1');
        EXECUTE format('create table if not exists t_tab %%t1(myval int)');
        while lv_count <= VAL
                loop 
                lv_in = lv_in + 1;
                insert into t_tab values(lv_in);
                lv_count = lv_count + 1;
                end loop;
        RETURN mybool;
end;

$$
LANGUAGE plpgsql;

error

ERROR:  syntax error at or near "%"
LINE 1: create table if not exists t_tab %t1(myval int)
                                         ^
QUERY:  create table if not exists t_tab %t1(myval int)
CONTEXT:  PL/pgSQL function fn_while_loop_upd_table(integer) line 8 at EXECUTE
SQL state: 42601

SQL and PLpgSQL doesn't do implicit variables replacement in strings. The syntax %%var is just wrong. The behaviour is similar to C or C++.

so

execute format('drop table if exists t_tab%%t1');
EXECUTE format('create table if not exists t_tab %%t1(myval int)');

has nothing common with reality. It should be

execute format('drop table if exists %I', 't_tab' || t1);
EXECUTE format('create table if not exists %I(myval int)', 't_tab' || t1);

t1 should be declared as int type (not varchar ).

PLpgSQL knows FOR cycle, what should be used, when number of iterations is known before:

CREATE OR REPLACE FUNCTION fn(n int)
RETURNS void AS $$
BEGIN
  FOR i IN 1..n
  LOOP
    EXECUTE format('drop table if exists %I', 't_tab' || i);
    EXECUTE format('create table if not exists %I(myval int)', 't_tab' || i);
  END LOOP;
END;
$$ LANGUAGE plpgsql;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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