0

I insert 1 rows successfully, but I want to insert rows using loop. The purpose is want to insert into table2 values of table1 that is not existed. My query like that:

FOR cl1 IN (SELECT tb1.cl1 FROM table1 tb1 WHERE tb1.cl1 NOT IN(SELECT tb2.cl1 FROM table2 tb2))
LOOP
  INSERT INTO table2 VALUES (cl1,'123456',sysdate);
END LOOP

1 Answer 1

1

No need for a loop:

INSERT INTO table2 
SELECT tb1.cl1, '123456', sysdate
FROM table1 tb1 
WHERE tb1.cl1 NOT IN(SELECT tb2.cl1 FROM table2 tb2)

Your sub-query using NOT IN will not work as you expect it, if the column cl1 contains NULL values.

It is also considered good coding style to explicitly list all columns in the insert statement: insert into table2 (col1, col2, col3) ...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.