CREATE TABLE ... AS SELECT与具有显式列类型的离散值

时间:2014-03-17 15:43:29

标签: sql sqlite

我想执行此声明

CREATE TABLE Tab2 AS SELECT 1, "abc", 123456789.12 UNION SELECT 2, "def", 25090003;

在SQLite数据库上。如何明确定义生成的列类型?

顺便说一句:这是关于CREATE TABLE ... AS SELECT with discrete values with explicit column names

的后续问题

1 个答案:

答案 0 :(得分:0)

根据documentation,列类型来自SELECT中结果列的expression affinities

1'abc'之类的普通值没有亲和力;你必须用CAST添加它:

CREATE TABLE Tab2 AS
SELECT CAST(1 AS INT) AS i,
       CAST('abc' AS TEXT) as t,
       CAST(123456789.12 AS REAL) AS r
UNION ALL SELECT 2, "def", 25090003 ...