2009-11-06 86 views
4

沒有發現我有一個表:插入行表,如果在另一臺

Employee (employeeID) 
EmployeeRank (rankID, employeeID) 

現在我有了另一個表所有會得到加薪的員工。

DueForRaise (rankID, employeeID) 

我需要插入所有處於DUeForRaise到EmployeeRank表,只有當他們尚不存在與同級別的員工。

我做這個更新的particlar rankID,@rankID。

會這樣嗎?

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

回答

13

如何:

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

感謝,一個巨大的表格,在您看來這是一個優化的查詢? – LightTechnician 2014-09-22 10:47:52

+1

這取決於你有指標。我將在(rankID,僱員) – Nestor 2014-09-22 19:58:00

-1

IF NOT EXISTS(SELECT * FROM EmployeeRank WHERE rankID = @rankID id和僱員= @EmployeeID)

INSERT INTO EmployeeRank(rankID,僱員)VALUES( @rankID,@EmployeeID)

+0

感謝指數EmployeeRank ......但僱員不是參數:) – mrblah 2009-11-06 05:29:43

相關問題