首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对包含两个独立表中的行的一个表执行大量SQL插入

如何对包含两个独立表中的行的一个表执行大量SQL插入
EN

Stack Overflow用户
提问于 2013-10-17 21:48:48
回答 1查看 60关注 0票数 2

我需要一些T-SQL帮助。我们有一个应用程序,跟踪分配给每个员工的培训需求(如CPR、急救等)。有一定的最低培训要求,所有员工都必须被分配,我的人力资源部希望我给予他们的能力,分配这些最低培训要求的所有人员,只要点击一个按钮。因此,我创建了一个名为TrainingRequirementsForAllEmployees的表,其中包含识别出的最小TrainingRequirements的TrainingRequirementID。

我希望将与TrainingRequirementsForAllEmployees中的每行连接的Employees表中的每个雇员插入表TrainingRequirementsForAllEmployees中的行。为了清晰起见,我将添加简短的表模式。

第一个表是Employees

代码语言:javascript
复制
EmployeeNumber  PK  char(6)
EmployeeName        varchar(50)

第二个表是TrainingRequirementsForAllEmployees

代码语言:javascript
复制
TrainingRequirementID     PK  int

第三个表(我需要插入的那个表)是Employee_X_TrainingRequirements

代码语言:javascript
复制
TrainingRequirementID  PK  int
EmployeeNumber         PK  char(6)

我不知道存储过程应该是什么样子才能达到我需要的结果。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-17 22:05:48

当需要两组数据的笛卡尔乘积时,cross join算子是合适的。因此,在存储过程的主体中,应该有如下内容:

代码语言:javascript
复制
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
    )
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19437925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档