0

我有以下语句,我想根据它的一些列条目更新表。我使用 Firebird 2.1,文档显示更新可以与 CTE 一起使用,但我的 Flamerobin 顽固地坚持认为更新语句无法识别。你能对此有所了解吗?

with cte as (select gen_id(gen_new,1) as num , N.elm_prof, N.elm_mat From 
(select distinct elm_mat, elm_prof from elements ) N )
update elements E set E.PROP_TYPE = cte.num where cte.elm_prof = E.ELM_PROF and cte.elm_mat = E.ELM_MAT

Engine Message :
Dynamic SQL Error
SQL error code = -104
Token unknown - line 3, column 1
update


merge into elements E1
using (with CTE as (select distinct e2.ELM_MAT mat1, e2.ELM_PROF mat2 from elements e2)
select gen_id(gen_new,1) num, mat1, mat2 from cte)
on E1.elm_mat = mat1 and e1.elm_prof = mat2
when matched then update set e1.prop_type = num
4

2 回答 2

1

文档中的哪些地方显示更新可以与 CTE 一起使用?当我查看Common Table Expressions时,它说

<cte-construct>  ::=  <cte-defs>
                      <main-query>

<main-query>     ::=  the main SELECT statement, which can refer to the
                      CTEs defined in the preamble

即只select允许作为main-query. 我认为您对声明感到困惑

当用括号括起来时,CTE 构造可以用作 SELECT 语句中的子查询,也可以用于 UPDATE、MERGE 等。

稍后在文档中。据我了解,这意味着您可以使用如下语句

UPDATE elements E
   SET E.PROP_TYPE = (CTE statement here) WHERE...

请注意,在这种情况下,CTE 必须是单例选择。

于 2016-01-13T07:29:42.213 回答
0

示例“CTE”,它不是一个 CTE,但它会帮助任何需要它的人。

merge into tribcfop t
using(
    with prodncm as (
    select a.iditemproduto, a.idncm, b.codigoncm, a.idtribcfop, c.piscstsai, c.cofinscstsai
    from produtoestoque a
    join ncm b on b.idncm = a.idncm
    join tribcfop c on c.idtribcfop = a.idtribcfop
    where b.codigoncm like '0201%')
    select prodncm.idtribcfop, prodncm.piscstsai, prodncm.cofinscstsai from prodncm
    ) a
on t.idtribcfop = a.idtribcfop
when matched then update set piscstsai = '06', cofinscstsai = '06'
于 2022-02-05T00:57:30.873 回答