postgres if ,when及判斷表是否存在的sql編寫

判斷表是否存在方法1:html

SELECT  case  WHEN   a.cc =1 THEN 2 else 0 END
FROM
(
select  count(*) as cc from pg_class where relname = 'wo'    --wo is table name,pg_class是pg自帶的關鍵字
) as a  

方法2:sql

用if判斷,但有如下條件post

The IF statement is part of the default procedural language PL/pgSQL. You need to create a function or execute an ad-hoc statement with the DO command.、spa

exist使用要求和if同樣postgresql

DO
$do$
BEGIN
IF (select  count(*) cc from pg_class where relname = 'wo') = 0 --必定要加括號
THEN
   INSERT INTO wo VALUES (2);
ELSE 
   INSERT INTO wo VALUES (1);
END IF;
END
$do$

 聯合使用select 和insert的方法以下code

create table language_ko(Key text,XML text);
insert into Language_ko(key,xml) (select "Key " as key,'<'||"Key"||'>'||"Korean"||'</'||"Key"||'>' as xml  from "centralClient1")
相關文章
相關標籤/搜索