I have the following CTE to update lTesterID column in table Employee to a valid ID randomly chosen from the table provider.
But I get this error
Cannot insert the value NULL into column 'lTesterID ', table 'Employee'; column does not allow nulls. UPDATE fails.
Even though there are no Null values as I'm excluding it in cteTable4.
Can someone point me in the right direction?
WITH cteTable3
AS ( SELECT ROW_NUMBER() OVER ( ORDER BY NEWID() ) AS n ,
lTesterID
FROM Employee
WHERE lTesterID= 0
),
cteTable4
AS ( SELECT ROW_NUMBER() OVER ( ORDER BY NEWID() ) AS n ,
ISNULL(lTesterID,0) AS lTesterID
FROM Provider
WHERE Active = 'True'
AND lTesterID IS NOT NULL
)
UPDATE cteTable3
SET lTesterID = ( SELECT lTesterID
FROM cteTable4
WHERE cteTable3.n = cteTable4.n
AND lTesterID IS NOT NULL
)