· 8 years ago · Jan 17, 2018, 08:10 AM
1create or replace function create_table() returns void as
2$$
3begin
4 if not exists(select * from pg_tables
5 where
6 schemaname = 'Public'
7 and tablename = 'test') then
8
9 create table test
10 (
11 the_id int not null,
12 name text
13 );
14
15 end if;
16
17end;
18$$
19language 'plpgsql';
20
21select creat_table();
22
23ERROR: relation "test" already exists
24CONTEXT: SQL statement "create table test
25 (
26 the_id int not null,
27 name text
28 )"
29PL/pgSQL function create_table() line 8 at SQL statement
30********** Error **********
31
32ERROR: relation "test" already exists
33SQL state: 42P07
34Context: SQL statement "create table test
35 (
36 the_id int not null,
37 name text
38 )"
39PL/pgSQL function create_table() line 8 at SQL statement