postgres:“BEGIN”或其附近的语法错误

时间:2016-09-13 12:33:08

标签: sql postgresql ddl

我读过这篇文章Check if table exists in SQL Server

并编写了这个脚本:

do $$
begin

IF (EXISTS (SELECT *
                 FROM INFORMATION_SCHEMA.TABLES
                 WHERE TABLE_SCHEMA = 'st'
                 AND  TABLE_NAME = 'config_change_log'))
BEGIN
   CREATE SCHEMA AUTHORIZATION st
   CREATE TABLE st.config_change_log
(
  id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
  last_config_version varchar(255),
  is_done Boolean,
  chage_description varchar(255)
)

END

end
$$

然而,当我跑步时:

psql -h localhost -d myDb -U myUser -f myScript.sql

我收到此错误:

psql:myScript.sql:21: ERROR:  syntax error at or near "BEGIN"

3 个答案:

答案 0 :(得分:1)

IF块看起来像这样:

IF <condition>
THEN
   <statements>
[ELSIF <condition>
THEN
   <statements>]
[ELSE
   <statements>]
END IF;

答案 1 :(得分:1)

这是你要找的吗?

BufferedInputStream

答案 2 :(得分:0)

这在Postgres中要容易得多,不需要IF

CREATE SCHEMA IF NOT EXISTS st;
CREATE TABLE IF NOT EXISTS st.config_change_log
(
  id serial NOT NULL PRIMARY KEY,
  last_config_version varchar(255),
  is_done Boolean,
  chage_description varchar(255)
);