Indicate a schema within an execution format

25 Views Asked by At

I combined a script to create tables with the same columns and lengths in a loop. The problem is that I want to create them in a specific schema, but I can't pinpoint it. They are currently being created as temporary tables.

Version: PostgreSQL 15.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4), 64-bit

/* ------------- CRIA A TABELA BOTS ------------- */
CREATE TEMPORARY TABLE DEF_BOTS (
LIN INT,
NOME VARCHAR(5)
);

INSERT INTO DEF_BOTS(NOME)
VALUES
('BOT_1'),
('BOT_2'),
('BOT_3');

UPDATE DEF_BOTS A
SET LIN=B.LIN
FROM (SELECT ROW_NUMBER() OVER(ORDER BY NOME) AS LIN, NOME FROM DEF_BOTS)B
WHERE A.NOME=B.NOME;

/* ------------- CRIA AS TABELAS TEST ------------- */
DO $$
DECLARE COUNTER INTEGER:=1;
    MAX_COUNTER INTEGER;
    TABELA TEXT;
BEGIN
SELECT MAX(LIN) INTO MAX_COUNTER FROM DEF_BOTS;
WHILE COUNTER <= MAX_COUNTER LOOP
SELECT NOME INTO TABELA FROM DEF_BOTS WHERE LIN=COUNTER;    
EXECUTE FORMAT('CREATE TEMPORARY TABLE %I(
"var1" VARCHAR (30),
"var2" VARCHAR(50),
"bigvar3" VARCHAR(65535),
"var4" VARCHAR(250),
"var5" VARCHAR(250),
"var6" VARCHAR(250),
"datetime1" TIMESTAMP,
"datetime2" TIMESTAMP
)','TB_TEST_' || TABELA);
COUNTER:= COUNTER + 1;
END LOOP;
END $$;

SELECT * FROM "TB_TEST_BOT_1"
1

There are 1 best solutions below

3
Frank Heikens On

This not possible for a temporary table, see the manual:

Temporary tables exist in a special schema, so a schema name cannot be given when creating a temporary table.