2017-06-02 114 views
0

這裏是我的存儲過程:如何檢查兩個日期是否在SQL存儲過程的某個日期範圍內?

ALTER Procedure [dbo].[SearchCreatedAssignments] 
(@LessonName NVarchar(MAX), @DateFrom date, @DateTo Date, @LocationCode NVarchar(MAX)) 
As 
BEGIN 
Select * 
from dbo.CreatedAssignments 
--IsNull replaces NULL with the specified replacement value. 
--IsNull(check_expression, replacement_value) 
--NullIf returns a null value if the two specified expressions are equal. 
--In other words, NULLIF returns the first expression if the two expressions are not equal. 
--If the expressions are equal, NULLIF returns a null value of the type of the first expression. 

where LessonName = IsNull(NullIf(@LessonName,''),LessonName) 
AND StartDate = IsNull(NullIf(@DateFrom,''),StartDate) 
AND EndDate  = IsNull(NullIf(@DateTo,''),EndDate) 
AND LocationCode = IsNull(NullIf(@LocationCode,''),LocationCode) 
AND status ='a' 

END 

正如你所看到的,此存儲過程設置,使用戶可以通過任何一個搜索標準,或者搜索條件的任意組合進行搜索(條件是列名),而將其他標準留空。例如,假設用戶只想通過lessonname搜索數據庫。這個存儲過程允許用戶做到這一點。它將返回與指定的lessonname匹配的所有結果,而不管其他條件是否保留爲空。所以,這裏是我的問題:我怎麼能保留這個邏輯,但返回的結果在哪裏的日期範圍內的startdateenddate?我知道我需要使用between關鍵字,但我不確定如何實現這一更改,同時保留現有的邏輯。任何幫助是極大的讚賞。

+0

Mysql或sql-server ....期待dbo。您正在使用sql-server ..請修復正確的標籤 –

+0

糟糕。我的錯。謝謝。 – ic3man7019

+0

我認爲你可以使用Dynamic Sql。只需組合查詢字符串。訪問此鏈接更多地提到,我的朋友:https://www.codeproject.com/Articles/20815/Building-Dynamic-SQL-In-a-Stored-Procedure – Tomato32

回答

1

如果你想要的開始日期和結束日期落在相同日期範圍內 - 即你@DateFrom和@DateTo參數 - 你可以做這樣的:

ALTER Procedure [dbo].[SearchCreatedAssignments] 
(@LessonName NVarchar(MAX), @DateFrom date, @DateTo Date, @LocationCode 
NVarchar(MAX)) 
As 
BEGIN 
Select * 
from dbo.CreatedAssignments 
--IsNull replaces NULL with the specified replacement value. 
--IsNull(check_expression, replacement_value) 
--NullIf returns a null value if the two specified expressions are equal. 
--In other words, NULLIF returns the first expression if the two expressions  are not equal. 
--If the expressions are equal, NULLIF returns a null value of the type of  the first expression. 

where LessonName = IsNull(NullIf(@LessonName,''),LessonName) 
AND StartDate BETWEEN IsNull(NullIf(@DateFrom,''),'1901-01-01') AND IsNull(NullIf(@DateTo,''),'2199-01-01') 
AND EndDate  BETWEEN IsNull(NullIf(@DateFrom,''),'1901-01-01') AND IsNull(NullIf(@DateTo,''),'2199-01-01') 
AND LocationCode = IsNull(NullIf(@LocationCode,''),LocationCode) 
AND status ='a' 

END 

請注意,我將您的IsNulls的第二個參數更改爲使用標量值而非實際列。我這樣做是爲了防止當一個日期參數保留空白時出現問題,這可能會給您提供錯誤順序的比較日期(例如StartDate BETWEEN 1/1/2017和1/1/2015)。

+0

我相信這是我一直在尋找的。你介意在這裏解釋邏輯嗎?到目前爲止,在測試中,我確定它的工作方式與我預期的一樣;我只需要將自己的思想包圍在邏輯上的事情上。 – ic3man7019

+0

它會檢查「@ DateFrom」和「@ DateTo」參數之間的開始日期和結束日期是否各自(單獨)。 如果'@ DateFrom'爲空或者是一個空字符串,它會替換'1/1/1901'。如果'@ DateTo'是'null'或者是一個空字符串,它會替代'1/1/2199'。因此,例如,如果過程是用'@ DateFrom'執行'6/2/2017'執行的,但是'@ DateTo'保留爲空白,程序將檢查開始日期和結束日期是否是'between 6/2/2017和1/1/2199'。 –

+0

非常好。謝謝。 – ic3man7019

0

您可以在每個字段之間的運算符之間使用。

ALTER Procedure [dbo].[SearchCreatedAssignments] 
(@LessonName NVarchar(MAX), @DateFrom date, @DateTo Date, @LocationCode NVarchar(MAX)) 
As 
BEGIN 
Select * 
from dbo.CreatedAssignments 
--IsNull replaces NULL with the specified replacement value. 
--IsNull(check_expression, replacement_value) 
--NullIf returns a null value if the two specified expressions are equal. 
--In other words, NULLIF returns the first expression if the two expressions are not equal. 
--If the expressions are equal, NULLIF returns a null value of the type of the first expression. 

where LessonName = IsNull(NullIf(@LessonName,''),LessonName) 
AND (StartDate BETWEEN @DateFrom AND @DateTo) AND 
AND (EndDate BETWEEN @DateFrom AND @DateTo) AND 
AND LocationCode = IsNull(NullIf(@LocationCode,''),LocationCode) 
AND status ='a' 

END 
+0

這不能回答我的問題。當其他標準留空時,您的答案無法返回結果。 – ic3man7019

相關問題