PostgreSQL: Create table if not exists AS

后端 未结 6 1287
不思量自难忘°
不思量自难忘° 2020-12-29 04:54

I\'m using PostgreSQL and am an SQL beginner. I\'m trying to create a table from a query, and if I run:

CREATE TABLE table_name AS
   (....query...)
<         


        
6条回答
  •  一生所求
    2020-12-29 05:20

    Use do :

    do $$ begin
    
    if not exists (  SELECT 1
       FROM   information_schema.tables 
       WHERE  table_schema = 'schema_name'
       AND    table_name = 'bla ') then
    
      create table schema_name.bla as select * from blu;
    end if;
    
    end $$;
    
    

提交回复
热议问题