Recently I wrote a bigger query than I usually write and I got a SQL query error using the Sp_msforeachdb. This query is running for every database in the instance of SQL Server. In addition the query is working if I make it smaller and I noticed that the Sp_msforeachdb has a character limitation of 2000 chars.
I tried to place it first in a variable nvarchar, varchar but without any luck.
So is there any way to increase this limitation and execute my full query?
SQL Server Sp_msforeachdb query character without limitation
When we write a query than is running for every database in the instance of SQL Server, we use Sp_msforeachdb.
If your query is bigger than 2000 chars, the query cannot work because Sp_msforeachdb has a character limitation of 2000 chars.
The solution is to re-create
MySp_myforeachdb. We usesp_helptextto see the definition ofsp_MSforeachdbandsp_MSforeach_worker, and we change to a new character limitation of 262144 chars.