【问题标题】:How to check whether stored procedure executed successfully如何检查存储过程是否执行成功
【发布时间】:2015-10-21 14:00:45
【问题描述】:

如果你有一个执行多个查询的存储过程,它不返回值并且没有错误处理,你如何确定存储过程在从 VB 代码调用时是否成功结束?

例如,如果存储过程是这样的:

create stored procedure some_procedure
    Insert  into ...
    delete ...
    update ... 
end

...而VB代码是这样的:

Cmd.ActiveConnection = cn
Cmd.CommandText = "some_procedure"
Cmd.CommandType = adCmdStoredProc
Set rs = Cmd.Execute
Debug.Print rs(0)

...那我怎样才能得到一个返回值,0 表示成功或1 表示失败?

【问题讨论】:

标签: stored-procedures vb6 ado


【解决方案1】:

我相信通过 Execute 语句传递可选的 RecordsAffected 参数对您有用。由于您对细节如此匮乏,因此您必须尝试一下,但这对我来说适用于 SQL 和 FoxPro OLEDB 提供程序。另外,为什么没有错误处理?恕我直言,代码中没有很可能产生错误的错误处理程序是自找麻烦。

...
Dim lngRecsAffctd As Long

Cmd.ActiveConnection = cn
Cmd.CommandText = "some_procedure"
Cmd.CommandType = adCmdStoredProc
Cmd.Execute lngRecsAffctd, ,adCmdStoredProc   'why set to a recordset when the sproc doesn't return anything?
Debug.Print lngRecsAffctd
...

Execute 命令和 RecordsAffected 参数的完整细节可以在msdn.microsoft.com/en-us/library/ms681559%28v=vs.85%29.aspx找到

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 2012-10-01
    • 1970-01-01
    相关资源
    最近更新 更多