我有如下的存儲過程:如果沒有開始日期和結束日期的記錄,存儲過程?
CREATE Procedure [dbo].[GetSleepData]
@period varchar(30),
@fitbit varchar(6),
@customdate datetime null,
@startdate datetime null,
@enddate datetime null
as
if(@period = 'week')
Begin
Set @customdate = Getdate()
Select Sleepkey, fitbitid,totalMinutesAsleep,totalSleepRecords,totalTimeInBed, datetime from sleep where fitbitid = @fitbit and
datetime <[email protected] datetime= @customdate - 7
End
else if(@period = 'month')
Begin
Set @customdate = Getdate()
Select Sleepkey, fitbitid,totalMinutesAsleep,totalSleepRecords,totalTimeInBed, datetime from sleep where fitbitid = @fitbit and
datetime<= @customdate and datetime >= @customdate - 30
End
這是工作的罰款。我需要在select語句中添加一個if子句。它應該是這樣的:
如果startDate和/或endDate沒有記錄,它應該添加一個記錄,其中的值爲零。
你應該看看這篇文章討論這樣的查詢與多個執行路徑。 http://sqlinthewild.co.za/index.php/2009/09/15/multiple-execution-paths/ –
爲什麼比較字符串而不是日期? – lad2025