Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sp_executesql or exec(@var) is too long. Maximum length is 8000

I have large queries so i cant use linked server in production by rules. i pass a varchar(max) which this has more than 8000 characters.

but sp_executesql does not support more than 8000 characters then how can i execute my string?

like image 994
angel Avatar asked Oct 02 '13 14:10

angel


People also ask

Is too long maximum length is 8000?

Error "Maximum length is 8000" occurs when querying an OpenEdge database via a Microsoft SQL Server Linked Server. SQL query being executed is longer than 8K. Using the Microsoft T-SQL OPENQUERY syntax to execute the SQL query. The character string that starts with '...' is too long.

What is the difference between Exec vs Sp_executesql?

Exec vs sp_executesql The main difference between the EXEC or EXECUTE operators and the sp_executesql built-in stored procedure is that the EXEC operator is used to execute a stored procedure or a SQL command passed as a string or stored within a variable.

What is exec Sp_executesql in SQL?

The sp_executesql is a built-in stored procedure in SQL Server that enables to execute of the dynamically constructed SQL statements or batches. Executing the dynamically constructed SQL batches is a technique used to overcome different issues in SQL programming sometimes.


1 Answers

nvarchar(max) should work on SQL Server 2008 or later.

Does this work?:

declare @sql nvarchar(max)
set @sql = N'select' + CONVERT(NVARCHAR(MAX),REPLICATE(' ', 8000)) + ' ''Above 8000 character limit test'''
exec sp_executesql @sql

If you're using a version before that, you may need to split the query into multiple variables:

How to use SQL string variable larger than 4000 character in SQL server 2005 Stored Procedure?

like image 176
My Stack Overfloweth Avatar answered Sep 21 '22 17:09

My Stack Overfloweth