转换将varchar转换为数据类型int时失败但没有任何内容定义为int

时间:2017-03-17 12:27:24

标签: sql-server tsql

我在运行它时在2个不同的数据库中进行联盟我收到此错误:

Msg 245,Level 16,State 1,Line 16 转换varchar值时转换失败' 12037.5'数据类型int。

但是我的列都没有整数类型?由于某种原因,它将Main选择类型更改为整数。

DECLARE @SAGESTARTDATE AS DATETIME,
        @SAGEENDDATE AS DATETIME,
        @SAGEITEM AS NVARCHAR(50),
        @SPSSTARTDATE AS DATETIME,
        @SPSENDDATE AS DATETIME,
        @SPSCOUNTRY AS NVARCHAR(50)

set     @sagestartdate = '2017-01-15'
set     @sageenddate = '2017-01-15'
set     @sageitem = 'diesel 50ppm'
set     @spsstartdate = '2017-01-15'
set     @spsenddate = '2017-01-15'
set     @spscountry = 'dRC'


Select FleetNo, TxDate, Max(STCode) as StCode, SageRev, SageQty, Max(SagePrj) as SagePrj, 
        MAx(SPSType) as SPSType, MAx(SPSModel) as SPSModel, MAX(SPSSite) as SPSSite, Max(SPSCountry) as SPSCountry, 
        SPSFuel
From (
        Select Strans.[Description] COLLATE SQL_Latin1_General_CP1_CI_AS as FleetNo 
                , Strans.TxDate as TxDate
                , StMstr.Code as STCode
                , Strans.[Reference] as SageRev     
                , Strans.Quantity as SageQty        
                , STrans.ProjectCode as SagePrj     
                , 0 AS SPSTYPE      
                , 0 AS SPSMODEL
                , 0 AS SPSSITE
                , 0 AS SPSCOUNTRY       
                , 0 as SPSFuel              

        from [KCS SARL].DBO._bvSTTransactionsFull STrans join
            [KCS SARL].DBO._bvStockFull StMstr on STrans.AccountLink = StMstr.StockLink

        Where Code = @SAGEITEM and Strans.TxDate BETWEEN @SAGESTARTDATE AND @SAGEENDDATE 

        UNION ALL

            Select  SPSTr.FleetNo as FleetNo
                    , SPSTR.ProdDate AS TxDate
                    , 0 as STCode
                    , 0 as SageRev
                    , 0 as SageQty
                    , 0 AS SagePrj      
                    , SPSTR.EqType AS SPSTYPE       
                    , SPSTR.ModelName AS SPSMODEL       
                    , SPSTR.SiteName AS SPSSITE
                    , SPSTR.Country AS SPSCOUNTRY       
                    , SPStr.Fuel as SPSFuel

        From [SPS].dbo.DataInputTotal SPStr
        Where SPStr.WCode = 102 and SPSTR.ProdDate BETWEEN @SPSSTARTDATE and @SPSENDDATE 
                AND SPSTR.Country = @SPSCOUNTRY
    ) as Fuel

    Group By FleetNo, TxDate, SageRev, SageQty, SPSFuel

    Order by FleetNo 

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

SPStr.WCode可能是varchar,102int。尝试用单引号包装102

此外,如果这些类型中的任何一个不是整数,联盟将尝试根据你的联合的这一部分将它们转换为整数:

, 0 AS SPSTYPE
, 0 AS SPSMODEL
, 0 AS SPSSITE
, 0 AS SPSCOUNTRY
, 0 as SPSFuel

, 0 as STCode
, 0 as SageRev
, 0 as SageQty
, 0 AS SagePrj  
DECLARE @SAGESTARTDATE AS DATETIME,
        @SAGEENDDATE AS DATETIME,
        @SAGEITEM AS NVARCHAR(50),
        @SPSSTARTDATE AS DATETIME,
        @SPSENDDATE AS DATETIME,
        @SPSCOUNTRY AS NVARCHAR(50)

set     @sagestartdate = '2017-01-15'
set     @sageenddate = '2017-01-15'
set     @sageitem = 'diesel 50ppm'
set     @spsstartdate = '2017-01-15'
set     @spsenddate = '2017-01-15'
set     @spscountry = 'dRC'


Select FleetNo, TxDate, Max(STCode) as StCode, SageRev, SageQty, Max(SagePrj) as SagePrj, 
        MAx(SPSType) as SPSType, MAx(SPSModel) as SPSModel, MAX(SPSSite) as SPSSite, Max(SPSCountry) as SPSCountry, 
        SPSFuel
From (
        Select Strans.[Description] COLLATE SQL_Latin1_General_CP1_CI_AS as FleetNo 
                , Strans.TxDate as TxDate
                , StMstr.Code as STCode
                , Strans.[Reference] as SageRev     
                , Strans.Quantity as SageQty        
                , STrans.ProjectCode as SagePrj     
                , '' AS SPSTYPE      
                , '' AS SPSMODEL
                , '' AS SPSSITE
                , '' AS SPSCOUNTRY       
                , '' as SPSFuel              

        from [KCS SARL].DBO._bvSTTransactionsFull STrans join
            [KCS SARL].DBO._bvStockFull StMstr on STrans.AccountLink = StMstr.StockLink

        Where Code = @SAGEITEM and Strans.TxDate BETWEEN @SAGESTARTDATE AND @SAGEENDDATE 

        UNION ALL

            Select  SPSTr.FleetNo as FleetNo
                    , SPSTR.ProdDate AS TxDate
                    , 0 as STCode
                    , 0 as SageRev
                    , 0 as SageQty
                    , 0 AS SagePrj      
                    , SPSTR.EqType AS SPSTYPE       
                    , SPSTR.ModelName AS SPSMODEL       
                    , SPSTR.SiteName AS SPSSITE
                    , SPSTR.Country AS SPSCOUNTRY       
                    , SPStr.Fuel as SPSFuel

        From [SPS].dbo.DataInputTotal SPStr
        Where SPStr.WCode = '102' and SPSTR.ProdDate BETWEEN @SPSSTARTDATE and @SPSENDDATE 
                AND SPSTR.Country = @SPSCOUNTRY
    ) as Fuel

    Group By FleetNo, TxDate, SageRev, SageQty, SPSFuel

    Order by FleetNo 

答案 1 :(得分:0)

PS C:\> Get-Something.ps1 –computerName SERVER1 –filePath C:\folder SERVER1 is your computer name! Your files are in C:\folder location 是你试图与Strans.[Description] COLLATE SQL_Latin1_General_CP1_CI_AS as FleetNo结合的varchar,它可能是int,这就是你将错误转换为varchar的原因SPSTr.FleetNo as FleetNo