开发者

Insert rows in table if not found in another table

开发者 https://www.devze.com 2022-12-10 04:47 出处:网络
I have a table: Employee (employeeID) EmployeeRan开发者_如何转开发k (rankID, employeeID) Now I have another table that has all the employee\'s that are going to get a raise.

I have a table:

Employee (employeeID)
EmployeeRan开发者_如何转开发k (rankID, employeeID)

Now I have another table that has all the employee's that are going to get a raise.

DueForRaise (rankID, employeeID)

I have to insert all the employees that are in DUeForRaise into the EmployeeRank table ONLY if they are not already there with the same rank.

I am doing this update for a particlar rankID, @rankID.

Would this work?

INSERT EmployeeRank ( rankID, employeeID)
SELECT rankID, employeeID
FROM DueForRaise dfr
      OUTER JOIN EmployeeRank er er.employeeid = dfr.employeeid)
WHERE dfr.rankID = @rankID


How about:

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


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消