T-SQL - 将数据插入父表和子表
笔记:
表变量由实时环境中的实际表替换。
EmployeeID和ParentEmployeeDepartmentID列的值并不总是相互匹配。实时环境在udt(tEmployeeData)中的记录多于4
目标:
udt(tEmployeeData)将被传递给过程
该过程应首先将数据插入@MainEmployee表中(并获取EmployeeIDs)
接下来,该过程应该将数据插入@ParentEmployeeDepartment表中(并获取ParentEmployeeDepartmentID) - 注意EmployeeID来自上一个输出。
然后,该过程应该基于DepartmentType(“A”=插入@ChildEmployeeDepartmentTypeA和“B”=插入@ChildEmployeeDepartmentTypeB)拆分子级数据。
程序应该运行得快(需要逐行操作)
输出:
@MainEmployee:
EmployeeID FirstName LastName
---------------------------------
1 Tom_FN Tom_LN
2 Mike_FN Mike_LN
3 Joe_FN Joe_LN
4 Dave_FN Dave_LN
@ParentEmployeeDepartment:
EmployeeID ParentEmployeeDepartmentID DepartmentType
-------------------------------------------------------
1 1 A
2 2 B
3 3 A
4 4 B
@ChildEmployeeDepartmentTypeA:
ParentEmployeeDepartmentID DepartmentBuilding DepartmentEmployeeLevel DepartmentTypeAMetadata
---------------------------------------------------------------------------------------------------------
1 101 IV Tech/IT
3 101 IV Tech/IT
@ChildEmployeeDepartmentTypeB:
ParentEmployeeDepartmentID DepartmentBuilding DepartmentEmployeeLevel DepartmentTypeAMetadata
----------------------------------------------------------------------------------------------------------
2 OpenH XII Med
4 OpenC XII Lab
我知道我可以使用OUTPUT插入后条款,并得到EmployeeID和ParentEmployeeDepartmentID,但我不知道如何插入正确的子记录与正确映射到父表右表。任何帮助,将不胜感激。
相关分类