6GB Table - 37Mi Rows - 'System.OutOfMemoryException' - SQL 2008 R2
System.OutOfMemory exception is a .NET exception which is basically telling you that the grid control experienced an OOM condition. I had blogged about this in detail in the past.

You could try the recommendations to trim down the result set which is definitely required in the intermediate levels of the procedure. But to circumvent the OOM condition in SSMS, you could either try with output to TEXT or output to a file or use SQLCMD to execute the same T-SQL procedure/command.