SqlCommand 查询的长度有什么限制
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是一个想法:
SQLServer 2000 的 VARCHAR 允许最多 8000 个字符,因此这可能有效:
PSeudoCode:
Here is a thought:
SQLServer 2000's VARCHAR allows up to 8000 characters, so this might work:
PSeudoCode:
SqlServer 2000 对即席查询有 4000 个字符的查询限制。
你能把它抽象成一个存储过程吗?
SqlServer 2000 has a 4000 character query limit for adhoc queries.
Can you abstract this into a stored procedure?
动态查询必读...动态 SQL 的诅咒与祝福,我强烈推荐您阅读。 这次可能对你没有帮助,但将来肯定会对你有帮助。引用
文章中的内容,以防万一。
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.
我遇到了针对 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.
根据我自己的经验,我发现,最初看起来是 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.
不要因为 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.