You can make sure that you do not insert duplicate information using the EXISTS clause.
For example, if you have a table with the name of clients with the primary key client_id, you can use the following statement:
INSERT INTO clients (client_id, client_name, client_type) SELECT supplier_id, supplier_name, 'advertising' FROM suppliers WHERE not exists (select * from clients where clients.client_id = suppliers.supplier_id);
This statement inserts multiple records with nested selections.
If you want to insert a single entry, you can use the following statement:
INSERT INTO clients (client_id, client_name, client_type) SELECT 10345, 'IBM', 'advertising' FROM dual WHERE not exists (select * from clients where clients.client_id = 10345);
Using a double table allows you to enter your values ββin the select statement, even if these values ββare not currently stored in the table.
from http://www.techonthenet.com/sql/insert.php
gehsekky May 01 '09 at 17:52 2009-05-01 17:52
source share