Try coding it as a huge SELECT statement, you might be surprised how little memory it uses.