Code:
CREATE TYPE dbo.tEmployeeData AS TABLE ( FirstName NVARCHAR(50), LastName NVARCHAR(50), DepartmentType NVARCHAR(10), DepartmentBuilding NVARCHAR(50), DepartmentEmployeeLevel NVARCHAR(10), DepartmentTypeAMetadata NVARCHAR(100), DepartmentTypeBMetadata NVARCHAR(100) ) GO CREATE PROC dbo.EmployeeImport (@tEmployeeData tEmployeeData READONLY) AS BEGIN DECLARE @MainEmployee TABLE (EmployeeID INT IDENTITY(1,1), FirstName NVARCHAR(50), LastName NVARCHAR(50)) DECLARE @ParentEmployeeDepartment TABLE (EmployeeID INT, ParentEmployeeDepartmentID INT IDENTITY(1,1), DepartmentType NVARCHAR(10)) DECLARE @ChildEmployeeDepartmentTypeA TABLE (ParentEmployeeDepartmentID INT, DepartmentBuilding NVARCHAR(50), DepartmentEmployeeLevel NVARCHAR(10), DepartmentTypeAMetadata NVARCHAR(100)) DECLARE @ChildEmployeeDepartmentTypeB TABLE (ParentEmployeeDepartmentID INT, DepartmentBuilding NVARCHAR(50), DepartmentEmployeeLevel NVARCHAR(10), DepartmentTypeBMetadata NVARCHAR(100)) -- INSERT CODE GOES HERE SELECT * FROM @MainEmployee SELECT * FROM @ParentEmployeeDepartment SELECT * FROM @ChildEmployeeDepartmentTypeA SELECT * FROM @ChildEmployeeDepartmentTypeB END GO DECLARE @tEmployeeData tEmployeeData INSERT INTO @tEmployeeData (FirstName, LastName, DepartmentType, DepartmentBuilding, DepartmentEmployeeLevel, DepartmentTypeAMetadata, DepartmentTypeBMetadata) SELECT N'Tom_FN', N'Tom_LN', N'A', N'101', N'IV', N'Tech/IT', NULL UNION SELECT N'Mike_FN', N'Mike_LN', N'B', N'OpenH', N'XII', NULL, N'Med' UNION SELECT N'Joe_FN', N'Joe_LN', N'A', N'101', N'IV', N'Tech/IT', NULL UNION SELECT N'Dave_FN', N'Dave_LN', N'B', N'OpenC', N'XII', NULL, N'Lab' EXEC EmployeeImport @tEmployeeData GO DROP PROC dbo.EmployeeImport DROP TYPE dbo.tEmployeeData Notes:
The table variables are replaced by real tables in live environment.
EmployeeIDandParentEmployeeDepartmentIDcolumns' values don't always match each other. Live environment has more records in the udt (tEmployeeData) than just 4
Goal:
The udt (
tEmployeeData) will be passed into the procedureThe procedure should first insert the data into the
@MainEmployeetable (and get theEmployeeIDs)Next, the procedure should insert the data into the
@ParentEmployeeDepartmenttable (and get theParentEmployeeDepartmentID) - noteEmployeeIDis coming from the previous output.Then, the procedure should split the child level data based on the
DepartmentType("A" = insert into @ChildEmployeeDepartmentTypeA and "B" = insert into @ChildEmployeeDepartmentTypeB).ParentEmployeeDepartmentIDfrom@ParentEmployeeDepartmentshould be used when inserting data into either@ChildEmployeeDepartmentTypeAor@ChildEmployeeDepartmentTypeBThe procedure should should run fast (need to avoid row by row operation)
Output:
@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 I know I can use the OUTPUT clause after the insert and get EmployeeID and ParentEmployeeDepartmentID, but I'm not sure how to insert the right child records into right tables with right mapping to the parent table. Any help would be appreciated.