I've got the solution now!
When a user logs in, the function setup_enrolments is used.
When the sync script is run, it's function sync_enrolments instead.
There is also a problem with the latter: see
http://tracker.moodle.org/browse/MDL-8550
The fix for the SQL statement works so that not all enrolments are removed.
Thanks again for your time Iñaki.
Mike
Enrolment
enrol_database_sync.php from command line
This discussion has been locked so you can no longer reply to it.