SQL Server hitting System.OutOfMemoryException but server has 10GB free SQL Server 2012 11.0.5058.0

I'm running a very large batch of queries (about 2700) to simulate a user doing X, Y and Z in our app. I have SQL Server's max memory set to 2147483647 MB. However when I run the query I get several messages like so: 

An error occurred while executing batch. Error message is: Exception of type 'System.OutOfMemoryException' was thrown.

When I look at the server it has 20GB of memory free and SQL Server is only using 4.8GB (and that number is stable). 

I've tried putting some GO statements in to no avail. I don't know where it is hitting the memory issues. None of the queries return more than a few rows. 

How can I get around this memory issue?
That exception usually doesn't refer to the SQL Server itself running out of memory, but instead the client workstation that is processing the results running out of memory.

You will generally see this if you are outputting a very large result set into the Output window.

To troubleshoot the issue (and possibly resolve it) allow SQL to write the results to a file rather than trying to put it all in the output window.