我正在尝试使用 INSERT INTO ... SELECT
语句将列从一个表复制到另一个表中,但我收到一条错误消息:
gis=> INSERT INTO places (SELECT 0 AS osm_id, 0 AS code, 'country' AS fclass, pop_est::numeric(10,0) AS population, name, geom FROM countries);
ERROR: invalid input syntax for integer: "country"
LINE 1: ...NSERT INTO places (SELECT 0 AS osm_id, 0 AS code, 'country' ...
SELECT
语句本身给出了我期望的结果:
gis=> SELECT 0 AS osm_id, 0 AS code, 'country' AS fclass, pop_est::numeric(10,0) AS population, name, geom FROM countries LIMIT 1;
osm_id | code | fclass | population | name | geom
--------+------+---------+------------+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 | 0 | country | 103065 | Aruba | 0106000000010000000103000000010000000A000000333333338B7951C0C8CCCCCC6CE7284033333333537951C03033333393D82840CCCCCCCC4C7C51C06066666686E0284000000000448051C00000000040002940333333333B8451C0C8CCCCCC0C18294099999999418351C030333333B3312940333333333F8251C0C8CCCCCC6C3A294000000000487E51C000000000A0222940333333335B7A51C00000000000F62840333333338B7951C0C8CCCCCC6CE72840
(1 row)
但不知何故,它看起来似乎变得很困惑,认为 fclass
列应该是一个整数,而事实上,它实际上是一个 character varying(20)
gis=> \d+ places
Unlogged table "public.places"
Column | Type | Modifiers | Storage | Stats target | Description
------------+------------------------+------------------------------------------------------+----------+--------------+-------------
gid | integer | not null default nextval('places_gid_seq'::regclass) | plain | |
osm_id | bigint | | plain | |
code | smallint | | plain | |
fclass | character varying(20) | | extended | |
population | numeric(10,0) | | main | |
name | character varying(100) | | extended | |
geom | geometry | | main | |
Indexes:
"places_pkey" PRIMARY KEY, btree (gid)
"places_geom" gist (geom)
我尝试将所有列转换为目标表所需的确切类型,但这似乎没有任何效果。
我可以在网上找到的此错误消息的所有其他实例似乎都是人们尝试使用空字符串作为整数,这与这里无关,因为我选择了一个常量字符串作为 fclass
.
最佳答案
您需要指定要插入的列名称:
INSERT INTO places (osm_id, code, fclass, population, name, geom) SELECT ...
如果不单独指定它们,则假定所有列都将被插入 - 包括您希望自动填充的 gid
。因此,'country'
实际上是通过当前的 INSERT
语句插入到 code
中的。
关于postgresql - Postgres varchar 列给出错误 "invalid input syntax for integer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40097217/