INSERT INTO AS_EMAIL_CONFIRMATIONS (usersIdTesis, code, createdAt, confirmed, EmpID, usersId)
SELECT A.USR_IdTesis AS usersIdTesis, 'XXX' AS code, 1742515200 AS createdAt, 'Y' AS confirmed, A.EmpID AS EmpID, 0 AS usersId 
FROM AS_USRPWD A
INNER JOIN Usuarios C on A.USR_IdTesis = C.UseID 
LEFT JOIN AS_EMAIL_CONFIRMATIONS B ON A.USR_IdTesis = B.usersIdTesis AND A.EmpID = B.EmpID AND B.confirmed = 'Y'
WHERE (A.UPW_UltimoAcceso IS NOT NULL OR C.UPerID = 'MST') AND B.id IS NULL AND A.USR_IdTesis IS NOT NULL

INSERT INTO AS_EMAIL_CONFIRMATIONS (usersIdTesis, code, createdAt, confirmed, EmpID, usersId)
SELECT A.USER_usuariotesis AS usersIdTesis, 'XXX' AS code, 1742515200 AS createdAt, 'Y' AS confirmed, A.EmpID AS EmpID, 0 AS usersId 
FROM AS_USUARIOS_COLABORADORES A
INNER JOIN Usuarios C on A.USER_usuariotesis = C.UseID 
LEFT JOIN AS_EMAIL_CONFIRMATIONS B ON A.USER_usuariotesis = B.usersIdTesis AND A.EmpID = B.EmpID AND B.confirmed = 'Y'
WHERE A.USER_iat IS NOT NULL AND B.id IS NULL AND A.USER_usuariotesis IS NOT NULL