我正在尝试使用 CTE 将一些随机数据插入表中 -
create table foo (id integer)
with x as (select random())
insert into foo (id)
select x from x
Run Code Online (Sandbox Code Playgroud)
这给出了一个错误:
ERROR: column "id" is of type integer but expression is of type record
只是 CTE 的select
作品:
with x as (select random())
-- insert into foo (id)
select x from x
Run Code Online (Sandbox Code Playgroud)
我也无法打字:
with x as (select random())
insert into foo (id)
select x::integer from x
Run Code Online (Sandbox Code Playgroud)
这给出了一个错误:ERROR: cannot cast type record to integer
。
出了什么问题以及如何解决?
select x from x
Run Code Online (Sandbox Code Playgroud)
表示选择整行作为列(请注意输出中的括号,而不是纯数字)。就像复合类型的值一样。
例如,
melkij=> select pg_am.* from pg_am limit 1;
oid | amname | amhandler | amtype
-----+--------+----------------------+--------
2 | heap | heap_tableam_handler | t
melkij=> select pg_am from pg_am limit 1;
pg_am
---------------------------------
(2,heap,heap_tableam_handler,t)
Run Code Online (Sandbox Code Playgroud)
要获取函数的值,您需要访问特定列,而不是整行。
with x as (select random()) select x.random from x;
-- or
with x as (select random() as r) select r from x;
-- or
with x(r) as (select random()) select r from x;
Run Code Online (Sandbox Code Playgroud)
您可以使用插入中的值
with x as (select random() as r)
insert into foo (id)
select r from x;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
45 次 |
最近记录: |