I need some advice on a somewhat complex SQL statement (at least complex for me)...
I have an insert statement, with a sub-select statement, that does includes a number of somewhat complex expression's on the same column (REGEXP_SUBSTR). I also want to do some comparison's on the expression results and skip a record if it already exists in the target table.
I thought that a CTE would make things easier.
Here's what I have so far:
insert into myfile (realfield1, realfield2)
with mycte (ctefield1, ctefield2)
as (
select
REGEXP_SUBSTR(otherfield1, :regexp, 1,1,'',1), -- 1st group from expr
REGEXP_SUBSTR(otherfield2, :regexp, 1,1,'',2) -- 2nd group from expr
from otherfile
where otherfield2 = 'somevalue'
)
select ctefield1, ctefield2
from mycte
where ctefield1 <> ' ' and
not exists (
select *
from myfile
where realfield1 = ctefield1 and realfield2 = ctefield2
)
;
The first time I run the statement it works, a record gets inserted.
The second time I run the statement it's not working ... a record gets inserted. The 'not exists' predicate doesn't appear to be identifying that the record already exists in the target file.
This mailing list archive is Copyright 1997-2026 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact
[javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.