2017-03-08 117 views
-1

你好我米使用此存儲過程來得到一些信息:2日期之間的SQL SELECT查詢

ALTER proc [dbo].[RevenusStaticAdvanced] 
@Days int 
as 
------------------------------------------------------------------------------------- 
select ISNULL(SUM(CAST(TotalPrice as int)),0) as 'TotalPrice', 
ISNULL(COUNT(Lavage_Orders.ID),0) as 'Total Commandes' 
from Lavage_Orders 
inner join LavageTypes on Lavage_Orders.LavageType=LavageTypes.ID 
WHERE DATEDIFF(Day,Arrive,GETDATE()) Between 0 and @Days 
------------------------------------------------------------------------------------------------ 
select ISNULL(COUNT(ID),0) as 'TotalCommandes' , ISNULL(SUM(CAST(TotalPrice as int)),0) as 'RevnusRepairs' 
from Repair_OrdersDetails 
WHERE DATEDIFF(Day,Date,GETDATE()) Between 0 and @Days 
------------------------------------------------------------------------------ 
select ISNULL(SUM(Qte),0) as 'SelledQte' , ISNULL(COUNT(ID),0) as 'TotalCommandes' , ISNULL(SUM(CAST(TotalPrice as int)),0) as 'RevnusAccessoires' 
from Accessoires_Order 
inner join Accessoires_OrderDetails on Accessoires_OrderDetails.orderID=Accessoires_Order.ID 
WHERE DATEDIFF(Day,Date,GETDATE()) Between 0 and @Days 
------------------------------------------------------------------------------------------------------ 
select ISNULL(Accessoires.ID,0), ISNULL(SUM(Accessoires_OrderDetails.Qte),0) as Selled from Accessoires 
inner join Accessoires_OrderDetails on Accessoires_OrderDetails.AccessoireID=ID 
inner join Accessoires_Order on Accessoires_Order.ID=Accessoires_OrderDetails.orderID 
WHERE DATEDIFF(DAY,Accessoires_Order.Date,GETDATE()) Between 0 and @Days 
GROUP BY Accessoires.ID 
ORDER BY Selled 
DESC 

這個完美的作品時,我給它的天數(從curret日) 所以我想將其更改爲在2日期之間,因此我將條件更改爲:

WHERE Date Between @Date1 and @Date2 

但是這似乎不起作用。

我在C#中傳遞的日期值,如:

public AdvancedStatics AdvancedStaticsView2(DateTime D1,DateTime D2) 
{ 
     param[0] = new SqlParameter("@Date1", SqlDbType.DateTime); 
     param[0].Value = D1; 
     param[1] = new SqlParameter("@Date2", SqlDbType.DateTime); 
     param[1].Value = D2; 
} 

已存儲日期在我的表像:

2017年1月11日14:20:48.177

+3

「這並不似乎是工作」是沒有問題的說明,爲什麼它似乎沒有工作?請注意,「BETWEEN」在兩側都是包含的,沒有時間組件的「datetime」將默認爲午夜。這意味着如果'@ Date1' ='21-03-2017'和'@ Date2' ='23-03-2017',則不會選擇值23-03-2017 14:20:48.177(它大於'23-03-2017 00:00:00.000')。 – HoneyBadger

+0

錯誤是C#代碼,沒有改變它'@ Days'參數 – bradbury9

+0

@HoneyBadger所以我怎麼能改變之間只需要幾天之間 – Huster

回答

0

您的參數不匹配存儲過程參數。

如果你想傳遞天數:

public AdvancedStatics AdvancedStaticsView2(DateTime D1,DateTime D2) 
{ 
     param[0] = new SqlParameter("@Days", SqlDbType.Int); 
     param[0].Value = 45; 
} 

在你想傳遞兩個日期的情況:

ALTER proc [dbo].[RevenusStaticAdvanced] 
(
    @Date1 DateTime, 
    @Date2 DateTime, 
) 
as 
+0

順便說一句,在運行時,你的c#代碼應該提出一個錯誤,抱怨缺少參數,它會更聰明地添加你得到的錯誤,以便人們可以幫助更好。 – bradbury9

+0

通過意思我傳遞這個值在我的C#我的意思是在第二節不是第一個 – Huster

+1

不理解你的評論。 – bradbury9