(1) If Source and Destination are on the same server
I think you can achieve this using an Execute SQL Task without a Foreach Loop container:
(a) Employee.EmpName = Department.EmpName
With CTE_1 AS (SELECT EmpName , MAX(DOJ) as mx FROM Department GROUP BY EmpName)
UPDATE T1
SET T1.DOJ = CTE_1.mx
FROM Employee T1 INNER JOIN CTE_1 ON CTE_1 EmpName = T1.EmpName;
(b) Employee.EmpName = Department.FirstName
If the Department table contains a full name and Employee table contains only first name you should use the following query:
With CTE_1 AS (CASE WHEN CHARINDEX(' ', EmpName) > 0
THEN SUBSTRING(EmpName, 1, LEN(EmpName) - CHARINDEX(' ', REVERSE(EmpName)))
ELSE EmpName END AS EmpName
, MAX(DOJ) as mx FROM Department GROUP BY CASE WHEN CHARINDEX(' ', EmpName) > 0
THEN SUBSTRING(EmpName, 1, LEN(EmpName) - CHARINDEX(' ', REVERSE(EmpName)))
ELSE EmpName END)
UPDATE T1
SET T1.DOJ = CTE_1.mx
FROM Employee T1 INNER JOIN CTE_1 ON CTE_1 EmpName = T1.EmpName;
(c) Department.FirstName Begins with Employee.EmpName
Or if you are looking to join using a begin with condition which is less accurate:
With CTE_1 AS (SELECT EmpName , MAX(DOJ) as mx FROM Department GROUP BY EmpName)
UPDATE T1
SET T1.DOJ = CTE_1.mx
FROM Employee T1 INNER JOIN CTE_1 ON CTE_1 EmpName LIKE T1.EmpName + '%';
(2) If Source and Destination are on different servers
(a) Create a Linked Server in SQL SERVER Management Studio
I think that the simplest way is to create a linked server in SSMS or by T-SQL for the Teradata database and perform on of the queries above.
(b) Use a staging table
Another way, is to import the department table to a temporary table in SQL Server, then perform one of the queries above.
(c) Use Lookup with an OLEDB Command
If you don't have permissions to create a linked server, or to import data to a temporary table, you can an OLE DB Source from department table with a similar command:
SELECT CASE WHEN CHARINDEX(' ', EmpName) > 0
THEN SUBSTRING(EmpName, 1, LEN(EmpName) - CHARINDEX(' ', REVERSE(EmpName)))
ELSE EmpName END AS EmpName
, MAX(DOJ) as mx FROM Department GROUP BY CASE WHEN CHARINDEX(' ', EmpName) > 0
THEN SUBSTRING(EmpName, 1, LEN(EmpName) - CHARINDEX(' ', REVERSE(EmpName))) ELSE EmpName END From Department
And use an OLEDB COMMAND (SQL Server connection) to perform the update operations row by rows, using a similar command:
Update Employee SET DOJ = ? WHERE EmpName = ?
And in Column Mappings Tab map the DOJ column to the first parameter and EmpName column to the second parameter. if you are new to OLE DB Command check the following example for more details.
References