I'm doing a function in PostgreSQL and I want to use it to check whether a table already exists or not in a database, and if it does not exist, I want to create the table.
The problem is in the function statement, which receives varchar
, which parameter will be used for SELECT relname
and also create
of the table.
Follow the function:
CREATE OR REPLACE FUNCTION verificarDb (tb varchar)RETURNS BOOLEAN as
$$
BEGIN
select relname from pg_class where relname = tb and relkind='r';
if not found then
CREATE TABLE tb
(
id integer,
nome varchar
);
return false;
end if;
if found then
return true;
end if;
END;
$$
LANGUAGE plpgsql;
select verificarDb('tabela');
Verification is not being done by the contents of the tb
parameter, but rather by using the string tb
. In create
is also using the acronym tb
, I want to use the table name passed by parameter, how do I?