1

I need to insert non-duplicated values... This "merge with self-reference" is not working as expected, but not generated a error message... Can I use similar thing?

MERGE INTO mydb.mytab AS Tref 
USING mydb.mytab AS T ON Tref.id=T.id
WHEN NOT MATCHED THEN INSERT VALUES (123, 'etc');

(update but not change the question) Seems that other construction, as suggested here, is

INTO INTO mydb.mytab 
  SELECT t1.* 
  FROM ( select stack(2, 123,'ect1',  20,'etc2') as (id,etc) ) t1
  LEFT OUTER JOIN mydb.mytab t2 ON t1.id = t2.id
  WHERE t1.id is null
;

it is not elegant (there are other?) but it is working (!). There is some alternative with MERGE?

leftjoin
  • 36,950
  • 8
  • 57
  • 116
Peter Krauss
  • 13,174
  • 24
  • 167
  • 304

1 Answers1

1

Merge will insert only if mydb.mytab does not contain matching rows

MERGE INTO mydb.mytab AS Tref 
USING  ( select stack(2, 123,'ect1',  20,'etc2') as (id,etc) ) AS T ON Tref.id=T.id
WHEN NOT MATCHED THEN INSERT VALUES (t.id, t.etc) --columns positions should match those in Tref 
;
leftjoin
  • 36,950
  • 8
  • 57
  • 116