Hi @Donald Trung ,
Welcome to Microsoft Q&A!
Exception of type 'System.OutOfMemoryException' was thrown
This error indicates that Management Studio is running out of memory, not the SQL Server service. Even if you installed 64-bit SQL Server, the SQL Server Management Studio executable is a 32-bit application. Therefore, it is limited to 2 GB of memory.
Please refer to this document: Exception when you execute a query in SQL Server Management Studio
Here is a method you can take:
Use sqlcmd Utility instead of SSMS to run the SQL queries. This method enables queries to be run without the resources that are required by the SSMS UI. Additionally, you can use the 64-bit version of Sqlcmd.exe to avoid the memory restriction that affects the 32-bit SSMS process.
Best regards,
Seeya
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.