Insert rows in a table if they are not found in another table

I have a table:

Employee (employeeID) EmployeeRank (rankID, employeeID) 

Now I have one more table in which there are all employees who are going to get a promotion.

 DueForRaise (rankID, employeeID) 

I need to insert all the employees who are in DUeForRaise into the EmployeeRank table ONLY if they do not already exist with the same rank.

I am doing this update for the rankID parameter, @rankID.

Will this work?

 INSERT EmployeeRank ( rankID, employeeID) SELECT rankID, employeeID FROM DueForRaise dfr OUTER JOIN EmployeeRank er er.employeeid = dfr.employeeid) WHERE dfr.rankID = @rankID 
+4
source share
2 answers

What about:

 insert into EmployeeRank select * from DueForRaise p where NOT EXISTS( SELECT * FROM EmployeeRank WHERE rankID=p.rankID and employeeID=p.employeeID ); 
+13
source

IF NOT EXISTING (SELECT * FROM EmployeeRank WHERE rankID = @rankID AND employeeID = @employeeID)

INSERT INTO EmployeeRank (rankID, employeeID) VALUES (@rankID, @employeeID)

-one
source

All Articles