执行此查询时,我收到错误,
数据类型varchar和日期在102行的add运算符中不兼容。你能帮忙吗?
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
答案 0 :(得分:2)
您应该在连接之前将日期转换为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