从存储过程返回的字符串值不起作用

时间:2017-04-25 08:53:02

标签: sql sql-server sqldatatypes

我创建了一个存储过程,它应该根据采取的操作返回一个字符串值。查询一切正常,除了我在@return_value上收到错误(“将varchar值转换为int)...我尝试使用查询来转换这两个值,但它不起作用...

我收到的错误:

    Conversion failed when converting the varchar value 'Request ID: 454 captured on 2017-04-25 10:16:07' to data type int.

这是我所做的SQL代码:

    USE [VehicleManagement]

    GO
    /****** Object:  StoredProcedure [dbo].[spSaveContractsCaptured]    
    Script Date: 2017/04/23 8:56:10 AM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[sp_SaveContractsCaptured]
    @VehicleServiceRequestID int,
    @InvoiceNo nvarchar(50),
    @Amount decimal(18, 2),
    @OdometerReading float,
    @ExpiryDate datetime,
    @ServiceDate datetime,
    @CapturedBy nvarchar(50),
    @CapturedDate datetime,
    @ContractStartDate datetime,
    @PeriodExpiry nvarchar(50),
    @returnVal nvarchar(255) output

    AS
  BEGIN
  SET NOCOUNT ON

  IF NOT EXISTS (SELECT ID FROM [VehicleManagement].[dbo].[VehicleService_Captured]
  WHERE VehicleServiceRequestID = @VehicleServiceRequestID )  
  BEGIN
        INSERT INTO [VehicleService_Captured] (VehicleServiceRequestID, InvoiceNo,
        Amount, OdometerReading, ExpiryDate, ServiceDate, CapturedBy, CapturedDate, ContractStartDate, PeriodExpiry)
        VALUES (@VehicleServiceRequestID, @InvoiceNo, @Amount, @OdometerReading, @ExpiryDate, @ServiceDate, 
        @CapturedBy, @CapturedDate, @ContractStartDate, @PeriodExpiry) 

        set @returnVal = SCOPE_IDENTITY()
        RETURN CAST(@returnVal AS VARCHAR(255))
    end
else
   SET @returnVal = (SELECT 'Request ID: ' + convert(varchar,ID) +  ' captured on ' + convert(varchar,[CapturedDate],120)  FROM [VehicleManagement].[dbo].[VehicleService_Captured]
    WHERE VehicleServiceRequestID = @VehicleServiceRequestID)

   RETURN CAST(@returnVal AS nvarchar(255))
 END

我缺少什么或者我做错了什么?

2 个答案:

答案 0 :(得分:2)

当Triv指向正确的方向时,您应该只设置输出变量的值并删除 RETURN

USE [VehicleManagement]

    GO
    /****** Object:  StoredProcedure [dbo].[spSaveContractsCaptured]    
    Script Date: 2017/04/23 8:56:10 AM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[sp_SaveContractsCaptured]
    @VehicleServiceRequestID int,
    @InvoiceNo nvarchar(50),
    @Amount decimal(18, 2),
    @OdometerReading float,
    @ExpiryDate datetime,
    @ServiceDate datetime,
    @CapturedBy nvarchar(50),
    @CapturedDate datetime,
    @ContractStartDate datetime,
    @PeriodExpiry nvarchar(50),
    @returnVal nvarchar(255) output

    AS
  BEGIN
  SET NOCOUNT ON

  IF NOT EXISTS (SELECT ID FROM [VehicleManagement].[dbo].[VehicleService_Captured]
  WHERE VehicleServiceRequestID = @VehicleServiceRequestID )  
  BEGIN
        INSERT INTO [VehicleService_Captured] (VehicleServiceRequestID, InvoiceNo,
        Amount, OdometerReading, ExpiryDate, ServiceDate, CapturedBy, CapturedDate, ContractStartDate, PeriodExpiry)
        VALUES (@VehicleServiceRequestID, @InvoiceNo, @Amount, @OdometerReading, @ExpiryDate, @ServiceDate, 
        @CapturedBy, @CapturedDate, @ContractStartDate, @PeriodExpiry) 

        set @returnVal = SCOPE_IDENTITY()
    END
ELSE
   SET @returnVal = (SELECT 'Request ID: ' + convert(varchar,ID) +  ' captured on ' + convert(varchar,[CapturedDate],120)  FROM [VehicleManagement].[dbo].[VehicleService_Captured]
    WHERE VehicleServiceRequestID = @VehicleServiceRequestID)

 END

当您调用该程序时,您应该声明一个变量并将其与“ out ”一起使用,如:

declare @message nvarchar(255)
exec [dbo].[sp_SaveContractsCaptured] //list parameteres, @returnval = @message out

另外请确保255就足够了。 希望它有所帮助,欢呼!

答案 1 :(得分:0)

返回仅用于给出错误级别。
您不应该返回结果,只有设置结果就足够了。

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/return-transact-sql

  

返回类型

     

可选择返回int。