khajlk
khajlk

Reputation: 851

Creating tables in postgres DB using plpgsql fucntion

Using pgAdmin III, I want to create tables in my postgres DB using a PL/pgSQL function specifically. The following set of commands does the job fine for me.

Create or Replace Function mk_tbl()
Returns Integer As $$
Declare
Begin
Drop Table if exists sel_streets;
Create Table sel_streets
(
id Integer,
Geom geometry
);
Create Index sel_streets_indx
ON sel_streets Using
gist (geom);
Return (1);
End $$ Language plpgsql volatile;

I am calling the function by this command:

Select mk_tbl();

My problem is every time I call this function, it creates tables in postgres DB as well as it returns '1' (in SQL editor output panel) which is not desired. I am aware that ultimately a function should return something and I can also use void construct like this:

Create or Replace Function mk_tbl()
Returns Void As $$
...

However, what I want is to call this function which should only create tables in my potgres DB and should return nothing. Is it possible to do this using PL/pgSQL specifically?

Upvotes: 0

Views: 95

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246413

What is the problem with an empty result?

Using a DO statement would do what you want:

DO LANGUAGE plpgsql $$BEGIN
   PERFORM mk_tbl();
END;$$;

Upvotes: 1

Related Questions