Thread: Create Table Problem

Create Table Problem

From
"Shavonne Marietta Wijesinghe"
Date:
Hello
 
I'm having some trouble checking if a table exist before i create it.
 
Example:
 
SELECT * FROM distributors
 
IF (table not found) THEN
    CREATE TABLE distributors (
        did     integer,
        name    varchar(40),
        UNIQUE(name)
    );
END IF
 
If i have a table like the above how can i identify if the table exists or not?
 
Thank You.
 
Shavonne Wijesinghe

Re: Create Table Problem

From
Volkan YAZICI
Date:
"Shavonne Marietta Wijesinghe" <shavonne.marietta@studioform.it> writes:
> IF (table not found) THEN
>     CREATE TABLE distributors (
>         did     integer,
>         name    varchar(40),
>         UNIQUE(name)
>     );
> END IF

Assuming you're in a procedure (above code snippet looks to be executed
within a PL/pgsql procedure):
 IF EXISTS (SELECT 1              FROM information_schema.tables             WHERE table_schema = 'public' AND
        table_name   = 'distributors)
 
THEN ...END IF;


Regards.