• Thank you all for looking a the post, got some help from a user and re-wrtten the code.

    In case anyone was interested, here is the fixed version

    FROM person p

    JOIN accessGroup ag ON p.accessGroup = ag.id

    JOIN accessGroupProperty agp ON ag.id = agp.accessGroupID

    JOIN timePeriod tp ON p.id = tp.personid

    JOIN BusIdentity bi ON tp.busIdentityId = bi.Id

    JOIN Context ctx ON ctx.currentValue = bi.description2

    WHERE

    agp.accessPropertyId = 1 -- access property is AccessLevel

    AND agp.content = 9 -- AccessLevel is Administrator

    AND p.status = 0 -- person is active

    AND tp.busIdentityId = @storeId

    AND tp.actualEndTime IS NULL

    AND ctx.subject = 'HeadOffice'

    AND ctx.parameter = 'Store'

    AND NOT EXISTS (SELECT 1 FROM #Operator_new opn WHERE p.logon = opn.operatorCode)