【问题标题】:The data types varchar and date are incompatible in the add operator数据类型 varchar 和 date 在 add 运算符中不兼容
【发布时间】:2016-07-21 10:31:39
【问题描述】:

在执行这个查询时,我得到一个错误,

第 102 行的 add 运算符中的数据类型 varchar 和 date 不兼容。您能帮忙吗?

ALTER PROCEDURE [dbo].[getrevenue] @date1 DATE
    ,@date2 DATE
    ,@StoreNo NVARCHAR(max)
AS
BEGIN
    DECLARE @sql_test NVARCHAR(max)

    --SET @StoreNo='68,78,104'
    SET @sql_test = 
        'SELECT  t1.transtoreno  As StoreNO ,t3.Name AS Name,
        t1.Dealdate AS Date,t1.UKEI as UKEI,
        t2.SubTotal AS SubTotal,
        t2.SubTotalDiscount AS SubToatlDiscount,
        t1.twoeyesSubtotalDiscount As TwoeyeSubTotalDiscount,
        t2.ValueInquiries AS TotalDiscount,
        t2.NetSale AS Netsale,
        t2.TotalSale AS ToatlSale,
        t2.Cash As Cash,
        t2.GiftVochuer As GiftVochuer,
        t2.Card AS Card,
        t2.Suica as Suica,
        t2.WONPOINT AS WAONPOINT,
        t1.TaxExemption AS TAXExemption,
        t2.TaxTotal AS TaxTotal,
        t2.Returngoods As Returngoods,
        t2.Regiminus As RegiMinus,
        t2.PrintRecipt As printrecipt,
        t1.Visitorcount As VisitorCount 



From 
(select         CAST( StoreNo as nvarchar) as transtoreno ,  (DealDate )as Dealdate,
                        SUM(SalePrice) AS UKEI,
                        SUM(TansuNebikiPrice)AS twoeyesdicount,
                        SUM(SubTotalNebiki2Price)AS twoeyesSubtotalDiscount,
                        SUM(TotalSalePrice-Si1Tax-RegiMinusNo)as Netsale,
                        SUM(SpecialConsumptionTaxPrice)AS TaxExemption ,
                        Sum(RegiMinusNo)AS Receiptissue,
                        SUM(VisitorCount)As Visitorcount
                        from                  POS_TtlTran
                        Group by StoreNo,DealDate)t1


left outer join 
  (Select Date as D, StoreNo as s,
   SUM(case when SerialNo like 23 then DayTotalAmt else 0 end) as Cash,
       SUM(case when SerialNo like 31 then DayTotalAmt else 0 end) as Card,
       SUM(case when SerialNo like 30 then DayTotalAmt else 0 end) as GiftVochuer,
       SUM(case when SerialNo like 138 then DayTotalAmt else 0 end) as Returngoods,
        SUM(case when SerialNo like 160 then DayTotalAmt else 0 end) as PrintRecipt,
         SUM(case when SerialNo like 304 then DayTotalAmt else 0 end) as Suica,
          SUM(case when SerialNo like 26 then DayTotalAmt else 0 end) as WONPOINT,
          SUM(case when SerialNo like 139 then DayTotalAmt else 0 end) as Regiminus,
          SUM(case when SerialNo like 4 then DayTotalAmt else 0 end) as SubToTal,
          SUM(case when SerialNo like 7 then DayTotalAmt else 0 end) as SubTotalDiscount,
          SUM(case when SerialNo like 8 then DayTotalAmt else 0 end) as TwoeyesubTotalDiscount,
          SUM(case when SerialNo like 18 then DayTotalAmt else 0 end) as ValueInquiries, 
          SUM(case when SerialNo like 22 then DayTotalAmt else 0 end) as TotalSale,
        SUM(case when SerialNo like 114 then DayTotalAmt else 0 end) as TaxTotal,
        SUM(case when SerialNo like 2 then DayTotalAmt else 0 end) as NetSale
              from POS_FinTtl
    Group By StoreNo,Date)t2

On t1.transtoreno = t2.s and t1.Dealdate=t2.D

Left outer JOIN
  (select StoreNo as No , StoreName As Name 
  from Store )t3

  On t2.s= t3.No

where (t1.transtoreno IN (' 
        + CAST(@StoreNo AS VARCHAR(100)) + ') and (t1.Dealdate between ' + CAST(@date1 AS DATE) + ' and ' + CAST(@date2 AS DATE) + ')'
END

【问题讨论】:

    标签: sql-server sql-server-2008 stored-procedures


    【解决方案1】:

    您应该在连接之前将日期转换为 varchar。试试这样,

    ALTER PROCEDURE [dbo].[getrevenue] @date1 DATE
        ,@date2 DATE
        ,@StoreNo NVARCHAR(max)
    AS
    BEGIN
        DECLARE @sql_test NVARCHAR(max)
    
        --SET @StoreNo='68,78,104'
        SET @sql_test = 
            'SELECT  t1.transtoreno  As StoreNO ,t3.Name AS Name,
            t1.Dealdate AS Date,t1.UKEI as UKEI,
            t2.SubTotal AS SubTotal,
            t2.SubTotalDiscount AS SubToatlDiscount,
            t1.twoeyesSubtotalDiscount As TwoeyeSubTotalDiscount,
            t2.ValueInquiries AS TotalDiscount,
            t2.NetSale AS Netsale,
            t2.TotalSale AS ToatlSale,
            t2.Cash As Cash,
            t2.GiftVochuer As GiftVochuer,
            t2.Card AS Card,
            t2.Suica as Suica,
            t2.WONPOINT AS WAONPOINT,
            t1.TaxExemption AS TAXExemption,
            t2.TaxTotal AS TaxTotal,
            t2.Returngoods As Returngoods,
            t2.Regiminus As RegiMinus,
            t2.PrintRecipt As printrecipt,
            t1.Visitorcount As VisitorCount 
    
    
    
    From 
    (select         CAST( StoreNo as nvarchar) as transtoreno ,  (DealDate )as Dealdate,
                            SUM(SalePrice) AS UKEI,
                            SUM(TansuNebikiPrice)AS twoeyesdicount,
                            SUM(SubTotalNebiki2Price)AS twoeyesSubtotalDiscount,
                            SUM(TotalSalePrice-Si1Tax-RegiMinusNo)as Netsale,
                            SUM(SpecialConsumptionTaxPrice)AS TaxExemption ,
                            Sum(RegiMinusNo)AS Receiptissue,
                            SUM(VisitorCount)As Visitorcount
                            from                  POS_TtlTran
                            Group by StoreNo,DealDate)t1
    
    
    left outer join 
      (Select Date as D, StoreNo as s,
       SUM(case when SerialNo like 23 then DayTotalAmt else 0 end) as Cash,
           SUM(case when SerialNo like 31 then DayTotalAmt else 0 end) as Card,
           SUM(case when SerialNo like 30 then DayTotalAmt else 0 end) as GiftVochuer,
           SUM(case when SerialNo like 138 then DayTotalAmt else 0 end) as Returngoods,
            SUM(case when SerialNo like 160 then DayTotalAmt else 0 end) as PrintRecipt,
             SUM(case when SerialNo like 304 then DayTotalAmt else 0 end) as Suica,
              SUM(case when SerialNo like 26 then DayTotalAmt else 0 end) as WONPOINT,
              SUM(case when SerialNo like 139 then DayTotalAmt else 0 end) as Regiminus,
              SUM(case when SerialNo like 4 then DayTotalAmt else 0 end) as SubToTal,
              SUM(case when SerialNo like 7 then DayTotalAmt else 0 end) as SubTotalDiscount,
              SUM(case when SerialNo like 8 then DayTotalAmt else 0 end) as TwoeyesubTotalDiscount,
              SUM(case when SerialNo like 18 then DayTotalAmt else 0 end) as ValueInquiries, 
              SUM(case when SerialNo like 22 then DayTotalAmt else 0 end) as TotalSale,
            SUM(case when SerialNo like 114 then DayTotalAmt else 0 end) as TaxTotal,
            SUM(case when SerialNo like 2 then DayTotalAmt else 0 end) as NetSale
                  from POS_FinTtl
        Group By StoreNo,Date)t2
    
    On t1.transtoreno = t2.s and t1.Dealdate=t2.D
    
    Left outer JOIN
      (select StoreNo as No , StoreName As Name 
      from Store )t3
    
      On t2.s= t3.No
    
    where (t1.transtoreno IN (''' 
            + CAST(@StoreNo AS VARCHAR(100)) + ''') and (t1.Dealdate between ''' + CAST(@date1 AS VARCHAR(30)) + ''' and ''' + CAST(@date2 AS VARCHAR(30)) + ''')'
    
        EXEC SP_EXECUTESQL @sql_test
    END
    

    【讨论】:

    • 很好的帮助:P 欣赏它。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多