将字符串转换为smalldatetime数据类型时转换失败

时间:2017-07-14 10:11:38

标签: sql-server stored-procedures

我在SQL Server 2008中遇到存储过程时遇到问题。存储过程如下所示:

ALTER PROCEDURE [dbo].[sp_UPLOAD_MOVTIT_Update]
( @IN_ID_RECORD             nvarchar(2)
, @IN_COD_IST               nvarchar(5)
, @IN_COD_SOC_GEST          char(5)
, @IN_COD_PTF               nvarchar(2)
, @IN_COD_LIN               nvarchar(2)
, @IN_COD_GEST              nvarchar(5)
, @IN_DAT_RIF               nvarchar(10)    
, @IN_DAT_VAL               nvarchar(10)
, @IN_ID_UPLOAD             nvarchar(2)
, @IN_COD_CAUS              nvarchar(6)
, @IN_VAL_COD_TIT           nvarchar(30)
, @IN_DIV_RIF               nvarchar(3)
, @IN_IMP                   numeric(25,3)
, @IN_IMP_RATEO             decimal(18,3)
, @IN_QTA                   numeric(18,5)
, @IN_PRZ                   decimal(21, 10) = 0
, @IN_FX                    decimal(18, 9)
, @IN_COD_MER               nvarchar(20)
, @IN_NOTE                  nvarchar(100))

AS      

DECLARE @@SQL                   nvarchar(max)
    , @@TABLE_NAME              nvarchar(45)
    , @@WHERE                   nvarchar(max)
    , @@DAT_RIF                 smalldatetime   
    , @@DAT_VAL                 smalldatetime

SET NOCOUNT ON   

SET @@TABLE_NAME = 'UPLOAD_MOVTIT'
SET @@TABLE_NAME = @@TABLE_NAME + '_' + @IN_COD_SOC_GEST 

SET @@WHERE = ' WHERE'

IF (LTRIM(@IN_ID_RECORD) <> '')
    SET @@WHERE = @@WHERE + ' AND ID_RECORD = '+@IN_ID_RECORD

IF (LTRIM(@IN_COD_IST) <> '')       
    SET @@WHERE = @@WHERE + ' AND COD_IST = @IN_COD_IST'

IF (LTRIM(@IN_COD_SOC_GEST) <> '')
    SET @@WHERE = @@WHERE + ' AND COD_SOC_GEST = @IN_COD_SOC_GEST'  

--IF (LTRIM(@IN_COD_PTF) <> '')
--  SET @@WHERE = @@WHERE + ' AND COD_PTF = @IN_COD_PTF'    

--IF (LTRIM(@IN_COD_LIN) <> '')     
--  SET @@WHERE = @@WHERE + ' AND COD_LIN = @IN_COD_LIN'

--IF (LTRIM(@IN_COD_GEST) <> '')        
--  SET @@WHERE = @@WHERE + ' AND COD_GEST = @IN_COD_GEST'  

IF (LTRIM(@IN_DAT_RIF) <> '')
    SET @@DAT_RIF = CONVERT(SMALLDATETIME, LTRIM(@IN_DAT_RIF), 103) 

IF (LTRIM(@IN_DAT_VAL) <> '')
    SET @@DAT_VAL = CONVERT(SMALLDATETIME, LTRIM(@IN_DAT_VAL), 103) 

IF (LTRIM(@IN_ID_UPLOAD) <> '')
    SET @@WHERE = @@WHERE + ' AND ID_UPLOAD = '+@IN_ID_UPLOAD   

IF (@@WHERE <> ' WHERE')        
    SET @@WHERE = REPLACE(@@WHERE, ' WHERE AND', ' WHERE')
ELSE
    SET @@WHERE = ''

BEGIN
    SET @@SQL = 'UPDATE ' + @@TABLE_NAME 
        + ' SET COD_PTF = @IN_COD_PTF
            , COD_LIN = @IN_COD_LIN
            , COD_GEST = @IN_COD_GEST
            , DAT_RIF = '+@@DAT_RIF+' 
            , DAT_VAL = '+@@DAT_VAL+'
            , COD_CAUS = @IN_COD_CAUS
            , VAL_COD_TIT = @IN_VAL_COD_TIT
            , DIV_RIF = @IN_DIV_RIF
            , IMP = @IN_IMP
            , IMP_RATEO = @IN_IMP_RATEO
            , QTA = @IN_QTA
            , PRZ = @IN_PRZ
            , FX = @IN_FX
            , COD_MER = @IN_COD_MER
            , NOTE = @IN_NOTE
            , DAT_ULT_MOD = CURRENT_TIMESTAMP ' + @@WHERE 

END

PRINT @@sql

EXEC sp_executesql @@SQL


, N'@IN_ID_RECORD nvarchar(2), @IN_COD_IST  nvarchar(5), @IN_COD_SOC_GEST char(5), @IN_COD_PTF nvarchar(2), @IN_COD_LIN nvarchar(2), @IN_COD_GEST nvarchar(5), @IN_DAT_RIF nvarchar(10), @IN_DAT_VAL nvarchar(10), @IN_COD_CAUS nvarchar(6), @IN_VAL_COD_TIT nvarchar(30), @IN_DIV_RIF  nvarchar(3), @IN_ID_UPLOAD nvarchar(2), @IN_IMP numeric(25,3), @IN_IMP_RATEO decimal(18,3), @IN_QTA numeric(18,5), @IN_PRZ  decimal(21, 10), @IN_FX decimal(18, 9), @IN_COD_MER nvarchar(20), @IN_NOTE nvarchar(100)'
                        , @IN_ID_RECORD, @IN_COD_IST, @IN_COD_SOC_GEST, @IN_COD_PTF, @IN_COD_LIN, @IN_COD_GEST, @IN_DAT_RIF, @IN_DAT_VAL, @IN_COD_CAUS, @IN_VAL_COD_TIT, @IN_DIV_RIF, @IN_ID_UPLOAD, @IN_IMP, @IN_IMP_RATEO, @IN_QTA, @IN_PRZ, @IN_FX, @IN_COD_MER, @IN_NOTE



RETURN @@ERROR

错误消息如下:

  

消息295,级别16,状态3,过程sp_UPLOAD_MOVTIT_Update,行91   将字符串转换为smalldatetime数据类型时转换失败。

起初我认为错误出现在这些方面:

SET @@DAT_RIF = CONVERT(SMALLDATETIME, LTRIM(@IN_DAT_RIF), 103) 

但经过一些调试后,我发现它是在这些方面:

            , DAT_RIF = '+@@DAT_RIF+' 
            , DAT_VAL = '+@@DAT_VAL+'

因为根据设计,必须完成更新的表将这两列设置为smalldatetime,并且存储过程在构建查询时将这两个变量转换为字符串。

任何人都知道如何解决这个错误?

编辑:这是我尝试使用的测试用例并返回错误:

USE [MYDB]
GO

DECLARE @return_value int

EXEC    @return_value = [dbo].[sp_UPLOAD_MOVTIT_Update]
        @IN_ID_RECORD = N'44',
        @IN_COD_IST = N'0',
        @IN_COD_SOC_GEST = N'90000',
        @IN_COD_PTF = N'20',
        @IN_COD_LIN = N'22',
        @IN_COD_GEST = N'00222',
        @IN_DAT_RIF = N'14/06/2017',
        @IN_DAT_VAL = N'18/04/2017',
        @IN_ID_UPLOAD = N'37',
        @IN_COD_CAUS = N'S',
        @IN_VAL_COD_TIT = N'IE00B1W4R501',
        @IN_DIV_RIF = N'USD',
        @IN_IMP = 1022924.19,
        @IN_IMP_RATEO = 0.00,
        @IN_QTA = 66000.00,
        @IN_PRZ = 15.50,
        @IN_FX = 1.07,
        @IN_COD_MER = N' ',
        @IN_NOTE = N' '

SELECT  'Return Value' = @return_value

GO

2 个答案:

答案 0 :(得分:0)

您可以使用 ISDATE()功能检查字符串是否有效日期

参考链接ISDATE()

将案例置于

__proto__

答案 1 :(得分:0)

我必须在语句的这一行之后添加变量:sp_executesql @@SQL

写下:

            , DAT_RIF = @@DAT_RIF 
            , DAT_VAL = @@DAT_VAL