使用 JDBC 和 null ResultSet 的临时表
我正在使用 MS SQL Driver 版本 3.0 通过标准 JDBC 连接执行存储过程。
我发现当我创建数据并将其插入临时表时,存储过程无法正确执行。
Java 代码不会引发异常,但 javax.sql.ResultSet
将为 null
。
存储过程中的失败点是当我取消注释 INSERT INTO #TBL CLM_NAME VALUES('VAL')
当我使用 SQL Studio Manager 执行该语句时执行顺利,数据符合预期。
有人遇到过这种情况或者知道为什么会这样吗?
最初我认为这是因为 SQL 驱动程序造成的,现在仍然认为是这样?强>
谢谢。
I am executing a stored procedure via standard JDBC Connection using MS SQL Driver version 3.0.
I have found that when I create and insert data into a temporary table the stored procedure doesn't execute properly.
The Java code won't throw a exception, but the javax.sql.ResultSet
will be null
.
The point of failure in the stored procedure is when I un-comment the INSERT INTO #TBL CLM_NAME VALUES('VAL')
When I execute the statement using SQL Studio Manager it executes without hassle and the data as expected.
Has anyone come across this or know why its the case?
Initially I thought its because of the SQL driver and I still think it is?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许这会有所帮助:
Maybe this will help:
executeQuery() 旨在与返回 ResultSet 的查询(通常是 SELECT 语句)一起使用。
executeUpdate() 用于返回更新计数的 INSERT、UPDATE、DELETE 或 DDL 语句。
上述两者(结果集和更新计数)都被 JDBC 视为“结果”。对于返回多个结果的查询,需要我们调用execute()。
如果存储过程使用临时表,它可能首先返回更新计数,然后返回结果集。您应该使用execute()来运行查询,调用getMoreResults()来跳过更新计数,然后调用getResultSet()来获取您想要的ResultSet。这里的问题是我们必须通过多次调用 getMoreResults() 来进行反复试验,才能通过调用 getResultSet() 来获取我们想要的结果集。
您可以通过指定“SET NOCOUNT ON”来抑制所有额外的“查询结果”,而不是上述反复试验的方法。
需要更改,
将您的逻辑放入 SP“SPCHILD”中。该存储过程将具有创建临时表的逻辑。
创建一个 SP“SPPARENT”,如下所示,
从父 SP“SPPARENT”中,您必须调用您实际的 SP,即“SPCHILD”。
从您的 JDBC 代码中对“SPPARENT”进行 SP 调用。
The executeQuery() is intended to be used with queries that return a ResultSet, typically SELECT statements.
The executeUpdate() is intended for INSERT, UPDATE, DELETE or DDL statements that return update counts.
Both of the above (ResultSets and update counts) are considered by JDBC to be as "results". For queries that return multiple results it requires we invoke execute().
If stored procedure makes use of temp tables, probably it returns an update count first, followed by a ResultSet. You should use execute() to run query, call getMoreResults() to skip the update count and then call getResultSet() to obtain the ResultSet you want. The problem here is we have to do trial and error by calling getMoreResults() multiple times to fetch result set we want by calling getResultSet().
Instead of above way of trial and error, you can suppress all the extra "query results" by specifying "SET NOCOUNT ON".
Changes required,
Put your logic in SP "SPCHILD". This Stored procedure will have logic along with temp table creation.
Create a SP "SPPARENT" as below,
From parent SP "SPPARENT" you have to call your actual SP i.e. "SPCHILD".
From your JDBC code make a SP call to "SPPARENT".
我也面临着同样的问题。为了解决这个问题,我将在我的 sql 服务器上设置跟踪,并在执行实际的 SP 调用之前查看 MS JDBC 驱动程序正在执行的所有语句。这应该可以帮助我理清事情。不幸的是我们的 DBA 今天不在,所以我明天必须在她的帮助下完成这件事。我会让你知道发生了什么以及解决办法是什么。
I am also facing the same issue. In order to fix this, I am going setup a trace on my sql server and see all the statements that are being executive by the MS JDBC driver before executing the actual SP call. This should help me clear things up. Unfortunately our DBA is out today so I will have to do it tomorrow with her help. I will let u know what happens and what is the fix.