You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: OutOfMemoryException Error When You Execute a Query
3
3
description: This article provides workarounds for the problem that occurs when you use SQL Server Management Studio (SSMS) to run a SQL query that returns a large amount of data.
4
-
ms.date: 10/29/2020
4
+
ms.date: 04/08/2025
5
5
ms.custom: sap:SQL Server Management, Query and Data Tools
6
+
ms.reviewer: jopilov
6
7
---
7
-
# Exception when you execute a query in SQL Server Management Studio
8
+
# OutOfMemoryException error when you execute a query in SQL Server Management Studio (SSMS)
8
9
9
10
This article helps you resolve the problem that occurs when you use SQL Server Management Studio (SSMS) to run a SQL query that returns a large amount of data.
When you use SSMS to run a SQL query that returns a large amount of data, you receive an error message that resembles the following:
17
+
When you use SSMS to run a SQL query that returns a very large amount of data, you receive an error message that resembles the following:
17
18
18
19
> An error occurred while executing batch. Error message is: Exception of type 'System.OutOfMemoryException' was thrown
19
20
20
21
## Cause
21
22
22
-
This issue occurs because SSMS has insufficient memory to allocate for large results.
23
-
24
-
> [!NOTE]
25
-
> SSMS is a 32-bit process. Therefore, it is limited to 2 GB of memory. SSMS imposes an artificial limit on how much text that can be displayed per database field in the results window. This limit is 64 KB in "Grid" mode and 8 KB in Text mode. If the result set is too large, the memory that is required to display the query results may surpass the 2 GB limit of the SSMS process. Therefore, a large result set can cause the error that is mentioned in the [Symptoms](#symptoms) section.
23
+
This issue occurs because SSMS has insufficient memory to allocate for large results. SSMS is a 32-bit process. Therefore, it's limited to 2 GB of user-mode virtual memory. SSMS imposes an artificial limit on how much text can be displayed per database field in the results window. This limit is 64 KB in Grid mode and 8 KB in Text mode. If the result set is too large, the memory that is required to display the query results may surpass the 2 GB limit of the SSMS process. Therefore, a large result set can cause the error that is mentioned in the [Symptoms](#symptoms) section.
26
24
27
25
## Workaround
28
26
@@ -47,31 +45,4 @@ Configure the query window to output the query results to a file. A file output
47
45
48
46
## Method 3: Use sqlcmd
49
47
50
-
Use [sqlcmd Utility](/sql/tools/sqlcmd-utility?redirectedfrom=MSDN&view=sql-server-ver15&preserve-view=true) 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.
51
-
52
-
## Applies to
53
-
54
-
- SQL Server 2012 Express
55
-
- SQL Server 2012 Developer
56
-
- SQL Server 2012 Enterprise
57
-
- SQL Server 2012 Standard
58
-
- SQL Server 2012 Web
59
-
- SQL Server 2008 R2 Datacenter
60
-
- SQL Server 2008 R2 Developer
61
-
- SQL Server 2008 R2 Enterprise
62
-
- SQL Server 2008 R2 Express
63
-
- SQL Server 2008 R2 Standard
64
-
- SQL Server 2008 R2 Web
65
-
- SQL Server 2008 R2 Workgroup
66
-
- SQL Server 2008 Developer
67
-
- SQL Server 2008 Enterprise
68
-
- SQL Server 2008 Express
69
-
- SQL Server 2008 Standard
70
-
- SQL Server 2008 Web
71
-
- SQL Server 2008 Workgroup
72
-
- SQL Server 2005 Developer Edition
73
-
- SQL Server 2005 Enterprise Edition
74
-
- SQL Server 2005 Evaluation Edition
75
-
- SQL Server 2005 Express Edition
76
-
- SQL Server 2005 Standard Edition
77
-
- SQL Server 2005 Workgroup Edition
48
+
Use [sqlcmd Utility](/sql/tools/sqlcmd/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.
0 commit comments