Skip to content

RETURNING clause in MERGE cannot reference column in aliased target table using qualified reference (alias.column) if DELETE action present [CORE6408] #6646

@firebird-automations

Description

@firebird-automations

Submitted by: @mrotteveel

The support for the RETURNING clause in MERGE is inconsistent, the types of column reference seem to depend on the specified actions in the WHEN clause.

Specifically, if the target table is aliased, then when a DELETE action is present, it is not possible to reference columns in the target table in the returning clause using alias.columnname.

As an example

create table dummy2 (
id integer constraint pk_dummy2 primary key,
val varchar(50)
);
commit;
insert into dummy2 (id) values (1);

The following statement will work fine

merge into dummy2 as d
using (select 1, 'ab' from rdb$database) as src(id, val)
on http://d.id = http://src.id
when matched then update set d.val = src.val
returning d.val, new.val, old.val, src.val

However if another a `WHEN MATCHED` clause is added with a DELETE action, then suddenly this doesn't work anymore:

merge into dummy2 as d
using (select 1, 'ab' from rdb$database) as src(id, val)
on http://d.id = http://src.id
when matched and http://d.id = 2 then delete
when matched then update set d.val = src.val
returning d.val, new.val, old.val, src.val

This results in
"""
Dynamic SQL Error; SQL error code = -206; Column unknown; D.VAL; At line 7, column 13 [SQLState:42S22, ISC error code:335544578]
"""

It is possible to reference the column unqualified (after renaming the val column in src to valsrc to avoid an ambiguous field name error):

merge into dummy2 as d
using (select 1, 'ab' from rdb$database) as src(id, valsrc)
on http://d.id = http://src.id
when matched and http://d.id = 2 then delete
when matched then update set d.val = src.valsrc
returning val, new.val, old.val, src.valsrc

I would expect the column in the target table to be referenceable as d.val in all these situations.

It is possible to use the table name if that target table wasn't aliased:

merge into dummy2
using (select 1, 'ab' from rdb$database) as src(id, val)
on http://dummy2.id = http://src.id
when matched and http://dummy2.id = 2 then delete
when matched then update set dummy2.val = src.val
returning dummy2.val, new.val, old.val, src.val

Commits: 1d2944c 2f3229c

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions