我写了一些过程代码,当它成功运行时,它们就是返回值$ git push -u origin master
Counting objects: 18, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (18/18), 165.52 KiB | 0 bytes/s, done.
Total 18 (delta 0), reused 0 (delta 0)
To github.com:Snowbabe/React-study.git
* [new branch] master -> master
。
但是当它在1
中运行时,它总是返回-1
(失败值)。
在这种情况下,我在ASP.NET Web API
中进行了测试。
SSMS
这将返回USE [MY_DATABASE]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[API_TO_WEB_CREATE_RESOURCE]
@RES_Size = 3019,
@RES_ContentType = N'image/jpeg',
@RES_OriginalName = N'evolving_google_identity_share.jpg',
@RES_GUID = N'b98bd7ee-cb19-49c8-a8dc-3b92b3210b91',
@RES_Path = N'~/Content/resources\\b98bd7ee-cb19-49c8-a8dc-3b92b3210b91',
@RES_Upload_USR_Index = NULL
SELECT 'Return Value' = @return_value
GO
1
中的相同代码。
ASP.NET Web API
这将返回int result = context.API_TO_WEB_CREATE_RESOURCE(
3019,
"image/jpeg",
"evolving_google_identity_share.jpg",
"b98bd7ee-cb19-49c8-a8dc-3b92b3210b91",
"~/Content/resources\\b98bd7ee-cb19-49c8-a8dc-3b92b3210b91",
null
);
这是我的程序。
-1
返回值USE [MY_DATABASE]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
ALTER PROCEDURE [dbo].[API_TO_WEB_CREATE_RESOURCE]
-- Add the parameters for the stored procedure here
@RES_Size int = 0,
@RES_ContentType nvarchar(100) = NULL,
@RES_OriginalName nvarchar(300),
@RES_GUID nvarchar(50),
@RES_Path nvarchar(500),
@RES_Upload_USR_Index int = NULL
AS
BEGIN
DECLARE @RES_RegisteredDatetime datetime = GETDATE()
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRAN
INSERT INTO
dbo.NS_Resource (RES_Size, RES_ContentType, RES_OriginalName, RES_GUID, RES_Path, RES_Upload_USR_Index)
VALUES (@RES_Size, @RES_ContentType, @RES_OriginalName, @RES_GUID, @RES_Path, @RES_Upload_USR_Index);
PRINT '리소스 추가됨'
COMMIT TRAN
RETURN(1);
END TRY
BEGIN CATCH
ROLLBACK TRAN
DECLARE @ERROR_Msg nvarchar = error_message()
RAISERROR('리소스 추가 중 문제 발생됨 %s', 16, 1, @ERROR_Msg)
RETURN (-1)
END CATCH
END
,并成功插入新值。
返回值1
,并成功插入新值。
返回值1
,并成功插入新值。
答案 0 :(得分:1)
在代码模块中包含多个return
语句绝不是一个好主意,SQL也不例外。尝试按照以下步骤重写您的过程,看看是否有帮助:
ALTER PROCEDURE [dbo].[API_TO_WEB_CREATE_RESOURCE]
-- Add the parameters for the stored procedure here
@RES_Size int = 0,
@RES_ContentType nvarchar(100) = NULL,
@RES_OriginalName nvarchar(300),
@RES_GUID nvarchar(50),
@RES_Path nvarchar(500),
@RES_Upload_USR_Index int = NULL
AS
DECLARE @RES_RegisteredDatetime datetime = GETDATE();
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @Ret int = -1; -- Failure by default
BEGIN TRY
BEGIN TRAN
INSERT INTO
dbo.NS_Resource (RES_Size, RES_ContentType, RES_OriginalName, RES_GUID, RES_Path, RES_Upload_USR_Index)
VALUES (@RES_Size, @RES_ContentType, @RES_OriginalName, @RES_GUID, @RES_Path, @RES_Upload_USR_Index);
PRINT '리소스 추가됨';
COMMIT TRAN
set @Ret = 1; -- Success
END TRY
BEGIN CATCH
if @@trancount > 0
ROLLBACK TRAN;
DECLARE @ERROR_Msg nvarchar(2048) = error_message();
RAISERROR('리소스 추가 중 문제 발생됨 %s', 16, 1, @ERROR_Msg);
END CATCH;
return @Ret;
go
但是,整个事情对我来说有点麻烦。如果您是从catch
块内部向客户端抛出错误,那么就足够了。它将转换为C#中的SQLException,因此返回值变得无关紧要。
我个人不使用返回值;相反,我通常创建2个输出参数,分别为数字int
和错误消息nvarchar(2048)
,并在catch
块中分配它们的值而无需重新抛出。执行完成后,我只需检查@ErrorNumber
输出参数的值,如果它既不为零也不为NULL,则在客户端处理错误。