执行顺序存储过程;在查询分析器中有效,但在我的 .NET 应用程序中无效

发布于 2024-08-27 05:26:20 字数 1368 浏览 6 评论 0原文

我有一个正在写入的审计记录表。我正在连接到 MyDb,它有一个名为“CreateAudit”的存储过程,它是到同一台计算机上另一个名为 MyOther DB 的数据库的直通存储过程,该数据库还有一个名为“CreatedAudit”的存储过程。

换句话说,在 MyDB 中我有 CreateAudit,它执行以下 EXEC dbo.MyOtherDB.CreateAudit。

我从应用程序中调用 MyDb CreateAudit 存储过程,使用 subsonic 作为 DAL。第一次调用它时,我使用以下内容(伪代码)来调用它:

            int openStatus, closeStatus = 0;
            openStatus = Convert.ToInt32(SPs.LogAccess(userId, "OPENED"));
            closeStatus = Convert.ToInt32(SPs.LogAccess(userId, "CLOSED"));

这是简化的,但这就是 LogAccess 所调用的:

 ALTER procedure [dbo].[LogAccess]
      @UserID           uniqueid,
      @Action           varchar(10),          
      @Status           integer output

 as

 DECLARE @mStatus INT

 EXEC [MyOtherDb].[dbo].[LogAccess]
      @UserID = @UserID,
      @Action = @Action,        
      @Status = @mStatus OUTPUT


 select @mStatus

在我的第二个存储过程中,它应该标记由 CreateAudit(recordId, "已打开”),状态为已关闭。

如果我独立地运行它们,或者即使我将它们粘贴到查询分析器中,这会非常有效。但是,当它们从应用程序执行时,记录不会标记为“已关闭”。

当我运行 SQL 探查器时,我看到两个查询都运行了,如果我复制查询并从查询分析器运行它们,记录将 100% 被标记为关闭!

当我从应用程序运行它时,大约每 20 次左右一次,记录成功标记为关闭 - 其他 19 次没有任何反应,但我没有收到错误!

.NET 应用程序是否可以跳过第一个存储过程的输出,并在创建第一个存储过程中的记录之前开始执行第二个存储过程?

当我将“WAITFOR DELAY '00:00:00:003'”添加到存储过程的顶部时,记录也 100% 关闭。

我的头在旋转,任何想法为什么会发生这种情况!

感谢您的回复,非常有兴趣了解这是如何发生的。

I have an audit record table that I am writing to. I am connecting to MyDb, which has a stored procedure called 'CreateAudit', which is a passthrough stored procedure to another database on the same machine called MyOther DB with a stored procedure called 'CreatedAudit' as well.

In other words in MyDB I have CreateAudit, which does the following EXEC dbo.MyOtherDB.CreateAudit.

I call the MyDb CreateAudit stored procedure from my application, using subsonic as the DAL. The first time I call it, I call it with the following (pseudocode):

            int openStatus, closeStatus = 0;
            openStatus = Convert.ToInt32(SPs.LogAccess(userId, "OPENED"));
            closeStatus = Convert.ToInt32(SPs.LogAccess(userId, "CLOSED"));

This is simplified, but this is what LogAccess calls:

 ALTER procedure [dbo].[LogAccess]
      @UserID           uniqueid,
      @Action           varchar(10),          
      @Status           integer output

 as

 DECLARE @mStatus INT

 EXEC [MyOtherDb].[dbo].[LogAccess]
      @UserID = @UserID,
      @Action = @Action,        
      @Status = @mStatus OUTPUT


 select @mStatus

In my second stored procedure it is supposed to mark the record that was created by the CreateAudit(recordId, "Opened") with a status of closed.

This works great if I run them independently of one another, or even if I paste them into query analyzer. However when they execute from the application, the record is not marked as "Closed".

When I run SQL profiler I see that both queries ran, and if I copy the queries out and run them from query analyzer the record gets marked as closed 100% of the time!

When I run it from the application, about once every 20 times or so, the record is successfully marked closed - the other 19 times nothing happens, but I do not get an error!

Is it possible for the .NET app to skip over the ouput from the first stored procedure and start executing the second stored procedure before the record in the first is created?

When I add a "WAITFOR DELAY '00:00:00:003'" to the top of my stored procedure, the record is also closed 100% of the time.

My head is spinning, any ideas why this is happening!

Thanks for any responses, very interested in hearing how this can happen.

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

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

发布评论

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

评论(1

苏辞 2024-09-03 05:26:20

在第一个存储过程中,尝试让 EXEC 语句等待第二个存储过程的返回值。我怀疑您的第一个 SP 正在触发第二个存储过程,然后立即将控制权返回给您的 .NET 代码,这导致了上述评论者的并发问题。 (也就是说,当您进行下一次数据库调用时,第二个 SP 尚未完成运行!)

SP1: EXEC @retval = SP2 ....

In your 1st stored proc, try having the EXEC statement wait for a return value from the 2nd stored proc. My suspicion is that your first SP is firing off the 2nd stored proc and then immediately returning control to your .NET code, which is leading to the above commenter's concurrency issue. (That is to say, the 2nd SP hasn't finished running yet by the time your next DB call is made!)

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