Postgresql INSERT查询时间戳返回错误:语法错误在或附近“:”SQL状态:42601字符:191

时间:2013-07-28 10:24:10

标签: postgresql sql-insert timestamp-with-timezone

表变更集的SQL是:

CREATE TABLE changesets
    (
      changeset_id bigint NOT NULL,
      user_id integer NOT NULL,
      created_at timestamp with time zone,
      closed_at timestamp with time zone,
      open boolean,
      min_lat double precision,
      min_lon double precision,
      max_lat double precision,
      max_lon double precision,
      CONSTRAINT pk_changeset PRIMARY KEY (changeset_id )
    )
    WITH (
      OIDS=FALSE
    );
    ALTER TABLE changesets
      OWNER TO postgres;

我尝试运行查询:

INSERT INTO changesets (changeset_id,user_id,open,min_lat,min_lon,max_lat,max_lon,created_at,closed_at) 
SELECT 16465146,1315463,false,27.6785105,85.3408257,27.6849711,85.350291,2013-06-08T06:29:51Z,2013-06-08T06:32:52Z 
WHERE NOT EXISTS (SELECT * FROM changesets WHERE changeset_id=16465146);

但它会引发错误:

ERROR:  syntax error at or near ":"
LINE 1: ...5105,85.3408257,27.6849711,85.350291,2013-06-08T06:29:51Z,20...
                                                             ^

********** Error **********

ERROR: syntax error at or near ":"
SQL state: 42601
Character: 191

如果我从SQL中删除created_at,closed_at目标列的时间戳值为2013-06-08T06:29:51Z,2013-06-08T06:32:52Z,则会成功执行sql。

这个SQL有什么问题?是否存在以下问题: - 时间戳?

谢谢,

1 个答案:

答案 0 :(得分:1)

将时间戳作为字符串文字传递 请参阅documentation

试试这个插页:

INSERT INTO changesets 
(changeset_id,user_id,open,min_lat,min_lon,max_lat,max_lon,created_at,closed_at) 
SELECT 16465146,1315463,false,27.6785105,85.3408257,27.6849711,85.350291,
      '2013-06-08T06:29:51Z','2013-06-08T06:32:52Z' 
WHERE NOT EXISTS (SELECT * FROM changesets WHERE changeset_id=16465146);

或甚至更好地给他们明确的类型,如:

INSERT INTO changesets (changeset_id,user_id,open,min_lat,min_lon,max_lat,max_lon,created_at,closed_at) 
SELECT 16465146,1315463,false,27.6785105,85.3408257,27.6849711,85.350291,
TIMESTAMP WITH TIME ZONE '2013-06-08T06:29:51Z', 
TIMESTAMP WITH TIME ZONE '2013-06-08T06:32:52Z' 
WHERE NOT EXISTS (SELECT * FROM changesets WHERE changeset_id=16465146);