SqlCommand 查询的长度有什么限制

发布于 2024-07-09 13:13:15 字数 364 浏览 5 评论 0原文

SQL Server 可以处理的查询长度是否有限制?

我有一个普通的 SqlCommand 对象,并将一个很长的 select 语句作为字符串传递。

该查询在针对 SQL Server 2005/2008 引擎运行时似乎没问题,但在 SQL Server 2000 引擎上则无法执行。

我没有任何错误详细信息,因为我只有第三手信息,但我的应用程序未按预期运行。 我可能会不厌其烦地安装一个 SQL Server 2000 实例,但我只是想知道是否有人有一个快速的。 是的,SQL Server 2000 中有 4K 或 8K 限制,但 2005 类型答案中没有。

我知道我可以使用存储过程,但假设我有不使用它们的正当理由:-)

Is there a limitation in the length of a query that SQL Server can handle?

I have a normal SqlCommand object and pass a very long select statement as a string.

The query seems to be fine when running against an SQL Server 2005/2008 engine but doesn't execute against an SQL Server 2000 engine.

I don't have any error details as I only have this information 3rd hand but my application isn't working as expected. I could go to the trouble of installing an SQL Server 2000 instance but I was just wondering if anyone has a quick. Yes there is a 4K or 8K limit in SQL Server 2000 but not in 2005 type answer.

I'm aware that I could use stored procedures but lets assume I have a valid reason for not using them :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

贱贱哒 2024-07-16 13:13:15

这是一个想法:

SQLServer 2000 的 VARCHAR 允许最多 8000 个字符,因此这可能有效:

PSeudoCode:

SQLCommand command = new SqlCommand("exec sp_executeSQL @CMD");
command.Parameters.Add(new SqlParameter("@CMD",YourDynamicSQL, VARCHAR);

Here is a thought:

SQLServer 2000's VARCHAR allows up to 8000 characters, so this might work:

PSeudoCode:

SQLCommand command = new SqlCommand("exec sp_executeSQL @CMD");
command.Parameters.Add(new SqlParameter("@CMD",YourDynamicSQL, VARCHAR);
沉睡月亮 2024-07-16 13:13:15

SqlServer 2000 对即席查询有 4000 个字符的查询限制。

你能把它抽象成一个存储过程吗?

SqlServer 2000 has a 4000 character query limit for adhoc queries.

Can you abstract this into a stored procedure?

靑春怀旧 2024-07-16 13:13:15

动态查询必读...动态 SQL 的诅咒与祝福,我强烈推荐您阅读。 这次可能对你没有帮助,但将来肯定会对你有帮助。引用

文章中的内容,以防万一。

SQL 2000 中的 sp_executesql 和长 SQL 字符串

SQL 2000 和 SQL 7 上的 sp_executesql 有一个限制,因为不能使用超过 4000 个字符的 SQL 字符串。 (在 SQL 2005 及更高版本上,您应该使用 nvarchar(MAX) 来避免此问题。)如果您想在查询字符串超出此限制时使用 sp_executesql 以利用参数化查询计划,实际上有一个解决方法。 也就是说,您可以将 sp_executesql 包装在 EXEC() 中:

声明 @sql1 nvarchar(4000),
@sql2 nvarchar(4000),
@状态字符(2)
选择@state = 'CA'
SELECT @sql1 = N'SELECT COUNT(*)'
SELECT @sql2 = N'FROM dbo.authors WHERE state = @state'
EXEC('执行 sp_executesql N''' + @sql1 + @sql2 + ''',
N''@state char(2)'',
@state = ''' + @state + '''')

这是可行的,因为 sp_executesql 的 @stmt 参数是 ntext,因此它本身没有任何大小限制。

您甚至可以通过使用 INSERT-EXEC 来使用输出参数,如下例所示:

创建表#result (cnt int NOT NULL)
声明 @sql1 nvarchar(4000),
@sql2 nvarchar(4000),
@状态字符(2),
@mycnt int
选择@state = 'CA'
SELECT @sql1 = N'SELECT @cnt = COUNT(*)'
SELECT @sql2 = N'FROM dbo.authors WHERE state = @state'
插入 #结果 (cnt)
EXEC('声明 @cnt int
EXEC sp_executesql N''' + @sql1 + @sql2 + ''',
N''@state char(2),
@cnt int 输出'',
@state = ''' + @state + ''',
@cnt = @cnt 输出
选择@cnt')
从#result中选择@mycnt = cnt

如果您认为这太混乱而不值得,我理解您。

a must read for dynamic queries... The Curse and Blessings of Dynamic SQL, I highly recommend you read it. Might not help you this time but it'll definitely help you in the future..

A quote from the article, just in case.

sp_executesql and Long SQL Strings in SQL 2000

There is a limitation with sp_executesql on SQL 2000 and SQL 7, since you cannot use longer SQL strings than 4000 characters. (On SQL 2005 and later, you should use nvarchar(MAX) to avoid this problem.) If you want to use sp_executesql when your query string exceeds this limit to make use of parameterised query plans, there is actually a workaround. To wit, you can wrap sp_executesql in EXEC():

DECLARE @sql1 nvarchar(4000),
@sql2 nvarchar(4000),
@state char(2)
SELECT @state = 'CA'
SELECT @sql1 = N'SELECT COUNT(*)'
SELECT @sql2 = N'FROM dbo.authors WHERE state = @state'
EXEC('EXEC sp_executesql N''' + @sql1 + @sql2 + ''',
N''@state char(2)'',
@state = ''' + @state + '''')

This works, because the @stmt parameter to sp_executesql is ntext, so by itself, it does not have any limitation in size.

You can even use output parameters by using INSERT-EXEC, as in this example:

CREATE TABLE #result (cnt int NOT NULL)
DECLARE @sql1 nvarchar(4000),
@sql2 nvarchar(4000),
@state char(2),
@mycnt int
SELECT @state = 'CA'
SELECT @sql1 = N'SELECT @cnt = COUNT(*)'
SELECT @sql2 = N'FROM dbo.authors WHERE state = @state'
INSERT #result (cnt)
EXEC('DECLARE @cnt int
EXEC sp_executesql N''' + @sql1 + @sql2 + ''',
N''@state char(2),
@cnt int OUTPUT'',
@state = ''' + @state + ''',
@cnt = @cnt OUTPUT
SELECT @cnt')
SELECT @mycnt = cnt FROM #result

You have my understanding if you think this is too messy to be worth it.

我遇到了针对 AS/400 运行的查询的 2k 限制。 我通常通过删除所有空格来设法低于 2k 限制 - 这会使查询不可读,但这是低于限制的最简单方法。

I've run into a 2k limit for queries run against an AS/400. I usually managed to get under the 2k limit by removing all whitespace - it makes the query unreadable but it's the simplest way to get under the limit.

甜嗑 2024-07-16 13:13:15

根据我自己的经验,我发现,最初看起来是 SQLServer2000 对查询长度的限制,实际上(无论你相信与否)并不是对查询长度的真正限制,而是对长度的限制查询中任何给定的 LINE。
大约一年前,当我遇到这个问题时,所以我突然不记得行长度是多少,但是您可以尝试将巨大的查询拆分为最大行长度 64K 或左右的行,并且看看进展如何。 我记得,不管你信不信,行长度限制可能是 64K。 我接受了这个超长的查询(由 sql-generator 程序生成),该查询大约有 80K 长,我在记事本中将其分成两半(即,我在 SQL 代码中大约中间点处添加了换行符) --- 但我确保不拆分任何单词),然后将整个内容粘贴到查询分析器命令窗口中。 然后它就起作用了,换行符位于中间的某个位置,从而导致 2 行中的每一行的长度都小于 64K。 我希望这有帮助。
如果不是,请尝试更短的线长度。 我确信,当我的查询达到其中没有行超过一定长度的程度时,整个查询就会起作用。

In my own experience, I found that, what first seemed to be a SQLServer2000 limit on the length of queries, was actually (believe it or not) not really a limit on the length of a query, but, is a limit on the length of any given LINE in the query.
It was about a year ago when I ran into this, so off the top of my head I don't remember what the line-length was, but you could try splitting the huge query into lines of max line length 64K or thereabouts, and see how it goes. My recollection is, that the line length limit might have been 64K, believe it or not. I had taken this insanely-long query (was generated by a sql-generator program), the query was about 80K long, and I split it in half in Notepad (i.e., I put a linefeed in the SQL code at about the halfway point --- but I made sure not to split up any words), and then pasted the whole thing into Query Analyzer command window. Then it worked, having the linefeed somewhere in the middle thus causing each of the 2 lines to be less than 64K long. I hope this helps.
If not, try smaller line lengths. I am certain that when I got my query to the point where no line within it, exceeded a certain length, the overall query worked.

别在捏我脸啦 2024-07-16 13:13:15

不要因为 sql 注入而这样做。 如果应用程序的动态 sql 完全可以由用户操作,则放弃此操作。

另外 - 考虑 SP,因为它更容易维护,并且还有助于 SQL 注入。

Do not to do this because of sql injections. Abandon this if the app's dynamic sql can be manipulated at all by the user.

also - consider SP's since its easier to maintain and it also helps with sql injection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文