我根据许多条件从数据库中取出记录,但现在我陷入了一个我想实现的条件,首先看代码。
ALTER PROCEDURE [dbo].[Report_SmsTwentyFourhours_sdpo_Sp]
@ParentID int
AS
BEGIN
Declare @MaxID bigint
Set @MaxID = (Select Max(ID) from SdpoReplies_SmsComplaints where ParentSmsID= @ParentID)
Declare @DateReceived DateTime
Set @DateReceived= (Select Convert(varchar(17),SdpoReplies_SmsComplaints.ReceivingDateTime, 113)
from SdpoReplies_SmsComplaints
where ParentSmsID= @ParentID
AND
SdpoReplies_SmsComplaints.ID = @MaxID)
Declare @DateSent DateTime
Set @DateSent = (Select SendMessages.SendingDateTime from SendMessages where BasicSmsID= @ParentID)
Declare @TimeFrame int
Set @TimeFrame = (Select DATEDIFF(hh, @DateSent, @DateReceived))
Declare @Subject varchar(MAX)
Set @Subject = (Select ReceivedMessages.Message from ReceivedMessages where ID= @ParentID)
Declare @FromMobileNo varchar(20)
Set @FromMobileNo = (Select ReceivedMessages.FromMobileNo from ReceivedMessages where ID= @ParentID)
Declare @PoliceStation varchar(50)
Set @PoliceStation = (Select CPOCMS.dbo.PoliceStations.PsName from CPOCMS.dbo.PoliceStations
Inner Join CmsSMSDb.dbo.SendMessages
ON CmsSMSDb.dbo.SendMessages.Psi_ID = CPOCMS.dbo.PoliceStations.Psid
where SendMessages.BasicSmsID= @ParentID)
Declare @SdpoContact varchar(50)
Set @SdpoContact = (Select CPOCMS.dbo.PoliceStations.sDpo_ContactNo from CPOCMS.dbo.PoliceStations
Inner Join CmsSMSDb.dbo.SendMessages
ON CmsSMSDb.dbo.SendMessages.Psi_ID = CPOCMS.dbo.PoliceStations.Psid
where SendMessages.BasicSmsID= @ParentID)
Declare @District varchar(20)
Set @District= (Select CPOCMS.dbo.Districts.Name from CPOCMS.dbo.Districts
Inner Join CPOCMS.dbo.PoliceStations
ON CPOCMS.dbo.PoliceStations.District_ID = CPOCMS.dbo.Districts.DistrictID
Inner Join CmsSMSDb.dbo.SendMessages
ON CmsSMSDb.dbo.SendMessages.Psi_ID= CPOCMS.dbo.PoliceStations.Psid
where CmsSMSDb.dbo.SendMessages.BasicSmsID= @ParentID)
Select @ParentID as SmsID, @DateReceived as ReceivingDate, @Subject as ComplaintSubject,
@FromMobileNo as IncommingNo, @PoliceStation as SDPOPoliceStation, @District as District ,
@SdpoContact as SDPOContact, @TimeFrame as TimeFrame
END
现在我想把它放在SdpoReplies_SmsComplaints.ReceivingDateTime
的基础上,就像日期之间的日期一样
怎么做得对?在这种情况下:
Select @ParentID as SmsID, @DateReceived as ReceivingDate, @Subject as ComplaintSubject,
@FromMobileNo as IncommingNo, @PoliceStation as SDPOPoliceStation, @District as District ,
@SdpoContact as SDPOContact, @TimeFrame as TimeFrame
答案 0 :(得分:0)
如果我理解正确,您希望在WHERE子句中放置谓词,以根据日期范围限制结果。要做到这一点,你只需要添加一行:
SELECT *
FROM SdpoReplies_SmsComplaints
WHERE
-- Add the following line to your WHERE clause:
ReceivingDateTime BETWEEN @Date1 AND @Date2
这将包括两个端点。请注意/如果@ Date1& @ Date2是与ReceivingDateTime不同的数据类型,如果您的某些数据从DATETIME隐式转换为DATE,则可以消除端点。