如何解决“游标未声明”的问题从 SQL Server
我通过 WebSphere 中的 Spring 连接到 SQL Server。我有一段非常简单的动态 SQL(如下),它抛出“游标未声明”。
SELECT TOP 1
short_desc shortDescription
FROM
product.prs.PRODUCT_SKN (nolock)
WHERE
prod_sales_div_code = 'XXX'
AND product_nbr = ?
似乎相关的数据源设置之一是“选择模式”是“光标”。我不太确定需要做什么来解决该错误。
编辑调用代码是: findShortDescriptionQuery.get().findObjectByNamedParam(of("productNumber", ProductId))
I am connecting to SQL Server via Spring in WebSphere. I have a piece of pretty simple dynamic SQL (below) that is throwing "The cursor was not declared."
SELECT TOP 1
short_desc shortDescription
FROM
product.prs.PRODUCT_SKN (nolock)
WHERE
prod_sales_div_code = 'XXX'
AND product_nbr = ?
One of the settings for the data source that seems relevant is the "select mode" is "cursor". I'm not really sure what I need to do to resolve the error.
edit The calling code is:findShortDescriptionQuery.get().findObjectByNamedParam(of("productNumber", productId))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试说
WITH (nolock)
,而不仅仅是(nolock)
。最新版本的 sql server 不再支持后一种语法。Try saying
WITH (nolock)
rather than just(nolock)
. The latter syntax is no longer supported on recent versions of sql server.我完全删除了“nolock”指令以及“TOP 1”,问题就消失了。这让我怀疑它与 JDBC 驱动程序有关。
这个解决方法不太理想,但足以满足我的目的。
I removed the "nolock" directive altogether, along with the "TOP 1", and the problem went away. This makes me suspect it is related to the JDBC driver.
This workaround is less than ideal, but will work just fine for my purpose.
检查您正在使用的服务器上的哪个数据库。如果同一服务器上有不同的数据库,如果您未指定,您的 sql 客户端通常会默认选择“master”或类似的数据库来运行查询。选择包含您正在查询或使用
USE的表的表GO
之前的查询将处理错误。Check which database on the server you're using. If you have different databases at the same server, your sql client will by default usually pick "master" or some such to run the query against if you don't specify. Selecting the one that contains the table you're querying or using
USE <dbname> GO
before the query will handle the error.我不知道这个答案在 10 年后可能有多大的相关性,但是在处理一些旧代码时我偶然发现了同样的“错误”,我通过从查询中删除字段列表来解决它,留下低效的
选择*。这为我解决了问题。
顺便说一句,旧代码使用 Microsoft SQL Server ODBC 驱动程序来访问 SQL Server,问题似乎正是如此,因为在 SSMS 上运行的相同查询有效没有问题”。
I don't know how much relevance this answer may have, after 10 years, but working on some old code I stumbled upon this same "error", and I solved it by removing the field list from the query, leaving the inefficient
SELECT *
. This solved the problem for me.BTW, the old code uses Microsoft SQL Server ODBC drivers to access the SQL Server, and it seems that the problem is exactly that, 'cause the same query run on SSMS works without issues'.
我最近有同样的问题..
通过用 CTE 重写所有查询解决了该问题。
目前正在寻找与从 Internet 信息服务调用的存储过程相同的问题。
I had recently same issue..
Solved the problem by rewriting all queries with CTE.
Currently looking for same issue with stored procedures called from Internet Information Services.