【发布时间】:2017-12-06 15:16:17
【问题描述】:
我如何知道在另一个过程中执行的存储过程是否返回错误或异常,即 void?
在这段代码中,我只显示了一个 (EXAMPLE),其中我有一个存储过程,并且在其中我从另一个存储过程中获取数据,但如果由于某种原因它返回空或在存储过程storedp_SearchTurn,因为我可以在返回主过程storedp_Process之前计算返回此过程的值。
ALTER PROCEDURE [dbo].[storedp_Process] (
@id numeric,
@type_time char(1)
)
As
declare @terminal varchar(30),
@id_employee numeric,
@hour datetime,
@date datetime,
@inic numeric
set @terminal = host_name()
set @inic = convert(numeric,replace(left(right(convert(varchar(80),getdate(),9),14),12),':',''))
-- Other stored procedure
exec storedp_SearchTurn @hour,
@turn output ,
@date_valid output
print @turn
Declare @period numeric(10)
Select @period = period from setup
where convert(char(10),date_valid,111)=convert(char(10),@date,111) and id_turn = @turn
--The code continues, this is just an example
--The code continues, this is just an example
--The code continues, this is just an example
--The code continues, this is just an example
--The code continues, this is just an example
因为即使storedp_SearchTurn 执行并返回空或发生某些事情,无论如何,如果sp_Process 中没有出现问题,storedp_Process 将返回 0。我需要捕获在另一个内部运行的过程的一些错误或异常。
我很好的获取输出值并在主程序中使用它们,但是我需要获取那些属于辅助程序的输出值,在C#中获取它们,以便在发生情况时通知用户在这个过程中。
在C#中我尝试将这些参数添加为输出,但是由于它们在执行时不属于主过程,所以异常跳转存储过程的参数比指定的参数多。
注意:存储过程完美运行,我只需要在C#中获取二级过程的那些输出值
但是我不能修改存储过程,公司就是这样创建的,它工作得很好,我只需要知道在某些时候输出值是否返回空,或者有数据,或者是否发生异常并获取这在 C# 中
我也找到了这些相关的主题,但没有一个回答我的问题:
How to return or get output value from stored procedure which is executing inside another proceed
Return Output Param of a Stored Procedure inside another Stored Procedure
Executing a stored procedure inside another stored procedure using Select Query
Get the value of a stored procedure inside another stored procedure with select
【问题讨论】:
-
没有人会一个接一个地阅读链接来帮助你。我根本不清楚你在这里想要完成什么。同时,我建议您停止使用 sp_ 作为前缀(或者甚至完全不使用前缀)。 sqlperformance.com/2012/10/t-sql-queries/sp_prefix
-
好的,谢谢,我会改进解释
-
我应该从相关主题中删除链接吗? ,我放置它们是为了让读者知道我花时间寻找信息。
-
那么让我看看我是否理解这一点。您有一个过程,它在执行过程中调用另一个过程。您的外部过程当前没有任何验证内部过程执行的方法。您需要以某种方式验证内部过程是否实际返回了一个值。但是您不得对任一程序进行任何更改。这听起来像一个准确的描述吗?
-
这就是我要告诉你的。你不能。您必须在主过程中将这些变量作为输出参数公开。或者我建议一个更好的主意是在内部过程中添加一些验证,如果内部过程中出现问题,则让它抛出异常。
标签: c# sql-server stored-procedures output