2016-05-30 51 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;