2
I am doing a function in Postgresql and I want to use it to check if a table already exists or not in a database and, if not, I want to create a table.
The problem is in the function declaration, which receives a varchar
, parameter that will be used for the SELECT relname
and also for the create
table.
Follows 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');
The check is not being done by the contents of the parameter tb
, but using the string tb
. In the create
is also using the acronym tb
, I want to use the table name passed by parameter, as I do?