2016-05-30 55 views
1

我有选择具有限定的可变WITH

with 
s1 as (select serial from wingstatushref where href = ? for key share), 
i1 as (insert into wingstatushref 
select ? 
where not exists(select * from s1) 
on conflict (href) do update set href=? returning serial) 

它使用相同的?三次。我试图整合?成与:

with 
h as (values (?)), 
s1 as (select serial from wingstatushref where href=h for key share), 
i1 as (insert into wingstatushref 
select h 
where not exists(select * from s1) 
on conflict (href) do update set href=h returning serial) 
select serial from s1 union all select serial from i1; 

但是,这给了我

x SQL Compiles and Typechecks 
x ERROR: column "h" does not exist 
    Position: 83 (specs2.scala:64) 

什么是从SELECT子句引用小时数据的正确方法是什么?

+0

'h'具有c CTE名称,语法上等同于一个表名称或相关名/别名。你必须指出你希望从'h'选择哪个字段:'从h ...'选择h.field。所以你必须提供一个字段名称给'VALUES'类。 (另外:'序列'看起来不像我的列名...) – wildplasser

回答

0

由于RhodiumToad从#postgresql

with 
    h(hr) as (values (?)), 
    s1 as (select serial from h, wingstatushref where href=h.hr for key share), 
    i1 as (insert into wingstatushref 
    select hr from h 
    where not exists(select * from s1) 
    on conflict (href) do update set href=wingstatushref.href where EXCLUDED.href=wingstatushref.href returning serial) 
    select serial from s1 union all select serial from i1;