[英]Get RETURN value from Proc
I have a SQL Server procedure which returns a result set, and is working well. 我有一个返回结果集的SQL Server过程,并且运行良好。 I am adding error handling (TRY/CATCH), and in the event of an error, want to RETURN -1 to indicate an issue (Or another negative integer to maybe know the reason for the failure). 我正在添加错误处理(TRY / CATCH),并且在发生错误的情况下,想要返回-1来指示问题(或者另一个负整数可能知道失败的原因)。
Is there a way to get the return value (As well as the result set, if required) from the procedure call? 有没有办法从过程调用中获取返回值(以及结果集,如果需要)?
I call the procedure like this: 我这样称呼程序:
Context.project_scheduled_event_payments(st.id);
In this case, I don't get a result set, as I don't need one. 在这种情况下,我没有结果集,因为我不需要一个结果集。 But I would like to know if the proc returned with NULL (All went fine), or a negative number indicating an issue: 但是我想知道proc返回的结果是否为NULL(一切正常),或者表示问题的负数:
IF(@EndDate IS NOT NULL)
BEGIN
BEGIN TRY
UPDATE scheduled_event_transaction
SET Deleted = GETUTCDATE(),
lastupdateuser = 0,
lastupdatedate = GETUTCDATE()
WHERE Scheduled_Event_ID = @scheduled_event_id
AND scheduled_payment_date > @EndDate
END TRY
BEGIN CATCH
ROLLBACK
RETURN -1
END CATCH
END
RETURN -- Success
As suggested by SystemOnline, refer get-return-value-from-stored-procedure-in-asp-net . 如SystemOnline所建议,请参考在ASP.NET中存储过程的get-return-value值 。
But I will suggest to throw error & catch in C#/vb code like: 但我建议抛出错误并捕获C#/ vb代码,例如:
BEGIN TRY
...
END TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
--@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
RAISERROR (@ErrorMessage, -- Message text.
16, --@ErrorSeverity,-- Severity.
@ErrorState -- State.
);
END CATCH;
Please note that I have explicitly set severity to 16 so that error is thrown to code. 请注意,我已将严重性显式设置为16,以便将错误引发给代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.