我需要一些T-SQL帮助。我们有一个应用程序,跟踪分配给每个员工的培训需求(如CPR、急救等)。有一定的最低培训要求,所有员工都必须被分配,我的人力资源部希望我给予他们的能力,分配这些最低培训要求的所有人员,只要点击一个按钮。因此,我创建了一个名为TrainingRequirementsForAllEmployees的表,其中包含识别出的最小TrainingRequirements的TrainingRequirementID。
我希望将与TrainingRequirementsForAllEmployees中的每行连接的Employees表中的每个雇员插入表TrainingRequirementsForAllEmployees中的行。为了清晰起见,我将添加简短的表模式。
第一个表是Employees
EmployeeNumber PK char(6)
EmployeeName varchar(50)第二个表是TrainingRequirementsForAllEmployees:
TrainingRequirementID PK int第三个表(我需要插入的那个表)是Employee_X_TrainingRequirements
TrainingRequirementID PK int
EmployeeNumber PK char(6)我不知道存储过程应该是什么样子才能达到我需要的结果。谢谢你的帮助。
发布于 2013-10-17 22:05:48
当需要两组数据的笛卡尔乘积时,cross join算子是合适的。因此,在存储过程的主体中,应该有如下内容:
insert into Employee_X_TrainingRequirements (TrainingRequirementID, EmployeeNumber)
select r.TrainingRequirementID, e.EmployeeNumber
from Employees e
cross join TrainingRequirementsForAllEmployees r
where not exists (
select 1 from Employee_X_TrainingRequirements
where TrainingRequirementID = r.TrainingRequirementID
and EmployeeNumber = e.EmployeeNumber
)https://stackoverflow.com/questions/19437925
复制相似问题