As much as I dislike just throwing code out there.
Insert into #While_Loop (@courseCodeList, @courseCodeList2)
You execute the varchar @begin as a sql script, so the variables you create inside it exist in the environment you ran it from. They are availible on their own after it is run, not as part of some micro-environment referenced by the varchar's name.
You should probably refactor that whole @begin business into a stored procedure that return the 2 variables you are interested in as output parameters.