I picked this up the other day, the sqlsrv_native limit_to_top_n function is just destroying SQL queries and just presenting mangled crap.
A simple way to reproduce this is to create a tet.php file with the following contents:
I've also attached a screenshot of what is happening with two queries one that selects the course id and shortname for all courses with limit 5, offset 5 and the second that does that same thing for all courses without a wiki activity.
The function is returning all columns for the orginal from table and drop all where, group by, and order by statement segments.
Let me know if there is any more information I can provide.