CodeKingPlusPlus
CodeKingPlusPlus

Reputation: 16081

Sql Server SSIS conditional insertion

I am trying to do an OLE DB Command to add rows to my table only when the primary key field does not already exist within the same table. This is what I have so far:

insert into employee
   (employee_id, first_name, middle_initial, last_name)   /*fields of the employee table*/
   values (employeedID, firstName, mInitial, lastName)    /*columns from my input       */

 /* only insert into the table where employee_ID is not already in the table */
 where ((select employee_id from employee where employee_id = employeeID) = NULL);  

Basically all I want is a conditional insertion statement.

Thanks!

Upvotes: 0

Views: 211

Answers (1)

Levi W
Levi W

Reputation: 815

I'm not exactly sure how your package is set up but you might consider using a staging table that doesn't have any constraints. Insert all your records into there first and then make a statement at the end that looks like:

insert into employee (employee_id, first_name, middle_initial, last_name) 
select t.employee_id, t.first_name, t.middle_initial, t.last_name
from temp_employee AS t
left join employee ON t.employee_id = employee.employee_id
where employee.employee_id is null

Upvotes: 1

Related Questions