DECLARE @PK_Department INT
DECLARE @UserIdNumber INT

DECLARE db_cur_per CURSOR LOCAL READ_ONLY FAST_FORWARD FOR
  SELECT UserIdNumber
  FROM Personel WITH (NOLOCK)
  WHERE Personel.UserTypeNumber IN (2,3,4) -- Techotel, AutoDay, AutoNight
OPEN db_cur_per

FETCH NEXT FROM db_cur_per INTO @UserIdNumber

WHILE @@FETCH_STATUS = 0
BEGIN
  DECLARE db_cur CURSOR LOCAL READ_ONLY FAST_FORWARD FOR
    SELECT Departments.PK_Department
    FROM Departments (NOLOCK)
  OPEN db_cur
  FETCH NEXT FROM db_cur INTO @PK_Department

  WHILE @@FETCH_STATUS = 0
  BEGIN
    IF NOT EXISTS
    (
      SELECT * 
      FROM PersonelDepartmentsXtab WITH (NOLOCK) 
      WHERE FK_Department = @PK_Department
      AND UserIdNumber = @UserIdNumber
    )
    BEGIN
      INSERT INTO PersonelDepartmentsXtab (UserIDNumber, FK_Department) VALUES (@UserIdNumber, @PK_Department)
    END
    FETCH NEXT FROM db_cur INTO @PK_Department
  END

  CLOSE db_cur
  DEALLOCATE db_cur

  FETCH NEXT FROM db_cur_per INTO @UserIdNumber
END

CLOSE db_cur_per
DEALLOCATE db_cur_per