I kindly suggest you to test this code against the original ask or Joerg's working code.
1. This post has just confirmed that nobody of TS visitors is interested in learning, or just trying to analyze something, except of getting a ready-to-copy-and-paste solution. Rare exceptions are: some moderators, and retired experts.
2. I really did not test specifically the last code because I reproduced this idea from my own 3-4 years old real production code, which was working perfectly that time, and successfully handled multi-GB unloaded databases before their transfer to another environment. I've already left that project, and I guess all needed data have been converted and transmitted since that time.
3. It is also a surprise for myself, that almost the same code is not working in full now. I can suspect any reason, even malfunctioning enhancement of SORT; no idea so far.
4. Anyone interested in learning multiple approaches to the same task might run a few tests by himself, to find out the updates needed for the working solution:
Joined: 15 Aug 2015 Posts: 1167 Location: Bamberg, Germany
We know that this forum lacks a Feedback Button. Some people think that this is "Put some request in - get a ready-to-copy solution out" only. I am not yet retired, get a lot of practice while reading and trying out here. Of course most provided samples can be enhanced in a certain way, but still this is a Help-Forum.