【问题标题】:How to return or get output value from stored procedure which is executing inside procedure main (get this in c#)如何从在过程 main 中执行的存储过程返回或获取输出值(在 c# 中获取)
【发布时间】: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

How to call stored procedure inside another stored procedure and set the values as in variable in DB2

Stored procedure executing another stored procedure

【问题讨论】:

  • 没有人会一个接一个地阅读链接来帮助你。我根本不清楚你在这里想要完成什么。同时,我建议您停止使用 sp_ 作为前缀(或者甚至完全不使用前缀)。 sqlperformance.com/2012/10/t-sql-queries/sp_prefix
  • 好的,谢谢,我会改进解释
  • 我应该从相关主题中删除链接吗? ,我放置它们是为了让读者知道我花时间寻找信息。
  • 那么让我看看我是否理解这一点。您有一个过程,它在执行过程中调用另一个过程。您的外部过程当前没有任何验证内部过程执行的方法。您需要以某种方式验证内部过程是否实际返回了一个值。但是您不得对任一程序进行任何更改。这听起来像一个准确的描述吗?
  • 这就是我要告诉你的。你不能。您必须在主过程中将这些变量作为输出参数公开。或者我建议一个更好的主意是在内部过程中添加一些验证,如果内部过程中出现问题,则让它抛出异常。

标签: c# sql-server stored-procedures output


【解决方案1】:

我如何知道在另一个过程中执行的存储过程是否返回错误或异常,即 void?

TRY...CATCH (Transact-SQL)
@@ERROR (Transact-SQL)
IS NULL (Transact-SQL)

你可以试试

ALTER PROCEDURE [dbo].[storedp_Process] (
...
BEGIN TRY  
    exec sp_SearchTurn    @hour,
                          @turn  output ,
                          @date_valid output
END TRY  
BEGIN CATCH  
    RAISERROR (0, 0, 0, 'An error has ocurred during sp_SearchTurn.')
    RETURN
END CATCH;   

IF @@ERROR != 0
BEGIN
    RAISERROR (0, 0, 0, 'An error has ocurred during sp_SearchTurn.')
    RETURN
END

IF @turn IS NULL OR @date_valid IS NULL
    RAISERROR (0, 0, 0, 'An error has ocurred during sp_SearchTurn.')
    RETURN
END

【讨论】:

  • 谢谢,但我不能修改存储过程,公司已经创建了它并且它工作得很好,我只需要知道在某些时候输出值是否返回空,或者有数据,或者如果发生异常,请在 c# 中查看
  • 您需要在上面的操作中指定此项。
  • 很抱歉给您带来不便
猜你喜欢
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 2010-10-16
  • 1970-01-01
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
相关资源
最近更新 更多