SELECT U.* INTO #PERMISOS_LOTES
FROM Seg_Door D, Seg_Access U
WHERE D.No = U.DoorNo
AND D.No = 248
AND (DenyFlag IS NULL OR DenyFlag = 0)

SELECT No INTO #NUEVO_PERMISO
FROM Seg_Door 
WHERE No in (                          
608, -- eD_AdmLoteCbtes
731, -- eD_Lote_Fact
732, -- eD_Lote_RemVta
733, -- eD_Lote_RemCpra
734, -- eD_Lote_Compras
735, -- eD_Lote_Recau
736) -- eD_Lote_Pagos

DECLARE @No_Nuevo_Permiso INT

DECLARE curNombre CURSOR FOR
SELECT No FROM #NUEVO_PERMISO

OPEN curNombre
FETCH NEXT FROM curNombre INTO @No_Nuevo_Permiso
WHILE @@FETCH_STATUS = 0
BEGIN

UPDATE #PERMISOS_LOTES SET DoorNo = @No_Nuevo_Permiso

INSERT INTO Seg_Access SELECT * FROM #PERMISOS_LOTES WHERE UserNo NOT IN (SELECT U.UserNo
																			FROM Seg_Door D, Seg_Access U
																			WHERE D.No = U.DoorNo
																			AND D.No = @No_Nuevo_Permiso)

FETCH NEXT FROM curNombre INTO @No_Nuevo_Permiso
END
CLOSE curNombre
DEALLOCATE curNombre