Subject | RE: [firebird-support] little help to build a query in Firebird |
---|---|
Author | Svein Erling Tysvær |
Post date | 2011-09-07T12:41:42Z |
I'm a complete novice regarding MERGE, never seen one, never used one. Though try something like:
merge into elements as e
using (with newtbl1 as
(select distinct elm_prof , elm_mat from ELEMENTS),
Newtbl2 (num, elm_prof, elmmat) as
(select gen_id(gen_new,1), n1.elm_prof, n1.elm_mat
From new_tbl1 n1)
Select n2.num, n2.elm_prof, n2.elmmat
From newtbl2 n2) a
on (e.elm_prof = a.elm_prof)
when matched then update set e.prop_type = a.num
I don't think the second CTE is required (but it shouldn't harm), and have no clue whether it will work as you hope for or if it will give you the same result as you've already seen.
Another thing is that I would recommend you not to use 'set generator', simply because it is DDL rather than DML and that there is a maximum number of times you can do this before you must do backup/restore. So, replace
set generator gen_new to 0;
with
select gen_id(gen_new, -gen_id(gen_new, 0)) from rdb$database
This will also reset the generator to 0, is DML and should will never stop working.
Hope it helps, even though I've no clue whether it will or not,
Set
merge into elements as e
using (with newtbl1 as
(select distinct elm_prof , elm_mat from ELEMENTS),
Newtbl2 (num, elm_prof, elmmat) as
(select gen_id(gen_new,1), n1.elm_prof, n1.elm_mat
From new_tbl1 n1)
Select n2.num, n2.elm_prof, n2.elmmat
From newtbl2 n2) a
on (e.elm_prof = a.elm_prof)
when matched then update set e.prop_type = a.num
I don't think the second CTE is required (but it shouldn't harm), and have no clue whether it will work as you hope for or if it will give you the same result as you've already seen.
Another thing is that I would recommend you not to use 'set generator', simply because it is DDL rather than DML and that there is a maximum number of times you can do this before you must do backup/restore. So, replace
set generator gen_new to 0;
with
select gen_id(gen_new, -gen_id(gen_new, 0)) from rdb$database
This will also reset the generator to 0, is DML and should will never stop working.
Hope it helps, even though I've no clue whether it will or not,
Set