存储过程将控制权传回得太快 - VB6

发布于 2024-12-29 03:43:06 字数 546 浏览 2 评论 0 原文

我有一个存储过程正在更新一个非常大的表(包含超过 1 亿条记录)。存储过程正在更新该表中的记录。

步骤如下:

  1. 将要更新的记录 ID 存储在记录集中(并非所有记录都会更新 - 仅大约 20000 条)
  2. 循环遍历记录集,并为记录集中的每个记录 ID 调用存储过程
  3. 每次存储过程完成时(对于第 1 部分中提到的记录集中的每条记录),更新表中的标志以表示更新已完成。

我发现一些奇怪的行为。看来存储过程在完成更新之前将控制权传递回 VB6,并继续处理下一条记录。存储过程稍后会超时(在另一个记录 ID 上)。因此,即使存储过程尚未运行(因为超时),也会有一些标志表示已更新(步骤 3)。这是正常行为吗?即存储过程在完成工作之前将控制权传递回 VB6?

我用 Google 搜索了一下,发现这可能是由于 SQL Server 优化存储过程的方式造成的。我希望控制权仅在更新完成后传递回 VB6。难道不是这样吗?

请注意,我意识到可能有更好的方法来解决这个问题。我的问题具体涉及 SQL Server 在完成工作(更新)之前将控制权传递回 VB6。

I have a stored procedure that is updating a very large table (with over 100 million records). The stored procedure is updating records in this table.

The steps are as follows:

  1. Store record IDs to be updated in a recordset (not all records will be updated - only about 20000)
  2. Loop through the recordset and call the stored procedure for each record ID in the recordset
  3. Each time the stored procedure has finished (for each record in the recordset mentioned in part 1), update a flag in a table to say that the update completed.

I am finding some strange behaviour. It appears that the stored procedure is passing control back to VB6 before it has completed its updates and is continuing processing the next record. The stored procedure is then timing out later on (on another record ID). Therefore there are flags that say updated (step 3), even though the stored procedure has not run (because it timed out). Is this normal behaviour i.e. for the stored procedure to pass control back to VB6 before it has finished the work?

I have Googled this and I have discovered that it could be because of the way the stored procedure is optimised by SQL Server. I would expect control only to be passed back to VB6 after the updates have completed. Is this not the case?

Please note that I realise there may be better ways of approaching this. My question specifically relates to SQL Server passing control back to VB6 before it has finished the work (update).

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

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

发布评论

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

评论(2

悍妇囚夫 2025-01-05 03:43:06

以下文章被证明是此问题的解决方案: http://weblogs.sqlteam.com/dang/archive/2007/10/20/Use-Caution-with-Explicit-Transactions-in-Stored-Procedures.aspx。似乎发生了以下行为:

1) 记录 1. 运行存储过程并创建事务。 SQL Command 对象发生超时。
2)记录2.存储过程运行成功。将控制权返回给 VB6 以更新数据库中的标志。
3)记录3.存储过程运行成功。将控制权返回给 VB6 以更新数据库中的标志。
4)记录4.存储过程运行成功。将控制权返回给 VB6 以更新数据库中的标志。
5) 程序结束。存储过程回滚事务(事务现在包含记录 1-4)。因此记录1-4不会被删除。

The following article proved to be the solution to this problem: http://weblogs.sqlteam.com/dang/archive/2007/10/20/Use-Caution-with-Explicit-Transactions-in-Stored-Procedures.aspx. It appears that the following behaviour was happening:

1) Record 1. Run stored procedure and create transaction. Timeout on SQL Command object occurrs.
2) Record 2. Run stored procedure successfully. Return constrol to VB6 to update flag in database.
3) Record 3. Run stored procedure successfully. Return constrol to VB6 to update flag in database.
4) Record 4. Run stored procedure successfully. Return constrol to VB6 to update flag in database.
5) Program ends. Stored procedure rolls back transaction (transaction now encompasses records 1-4). Therefore records 1-4 are not deleted.

那伤。 2025-01-05 03:43:06

你能...

  • 在 sql management studio 中运行代码并看看会发生什么并报告回来吗?如果是这样,我将更新这个答案,因为这将帮助我们了解它是代码/连接还是sql。

其他需要调查的事情,考虑到我们不知道您测试过哪些情况...

  • 在您的 vb 应用程序中使用相同的代码路径,并仅将存储过程中的 sql 更改为非常简单的内容,但就其内容而言具有相同的签名执行(即/如果有读取则进行基本读取,如果有删除则进行基本删除,更新和添加相同)看看会发生什么。

另外,还有一些其他的想法...

  • 如果您使用 MSSQL,那么就像某人打开查询窗口并绑定数据库一样简单。这很容易测试。我以前也遇到过同样的麻烦。我之前运行过没有超时的存储过程,通常会立即运行,但会静置过夜而不运行。才意识到另一个人打开了他们的查询窗口。关上窗户,噗的一声,它终于运行了。检查一下,这可能是表锁。无论是应用程序执行此操作,还是由另一个用户对数据库进行查询来执行此操作。检查以确保您的应用程序在每次使用数据库时关闭与数据库的连接。

Can you...

  • run the code in sql management studio and see what happens and report back? if so i will update this answer as that will help us understand if its the code / connection or sql.

other things to investigate, given we dont not what cases you have tested for...

  • use the same code path in ur vb application and change only the sql in the stored procedure to something very simple but has the same signature as far as what its doing (ie/ basica reading if there is reading, basic deleting if there is deleting, and same for updating and adding) to see what happens.

Also, some other thoughts...

  • if you are using MSSQL, it's as simple as someone leaving a query window open and it ties up the database. This is easily tested. I've had the same trouble before. I've run stored procedures before that had no timeout, that normally would run immediately but would sit overnight and not run. Only to realize another person left their query window open. Close their window and poof it finally runs. Check this out, it could be a table lock. Whether it be the application doing it, or it is being done by another user making queries to the DB. Check to make sure your application is closing connections to the DB each time their being used.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文