简体   繁体   中英

how to check SQL table with last one week data in sql server using stored procedure

my transaction_tbl structure like this

transactID   int
TBarcode     varchar(20)
Locid         int
PlateNo       varchar(20)
dtime         datetime
DelEcode     nvarchar(50)
Paydate     datetime
KeyRoomDate  datetime
DelDate      datetime
Status      int

i have a stored procedure like this:

 ALTER procedure [dbo].[IBSFVRIPodiumG]
       @locid INTEGER = NULL
    AS BEGIN
       SET NOCOUNT ON

       DECLARE @TodayMinus7Days DATETIME
    Declare @krrt integer
    Declare @DT integer
       SET @TodayMinus7Days = getdate()-7

       SELECT  
           t.TBarcode, t.PlateNo, t.DelEcode,cast(t.Paydate as Time) [REQ],
           datediff(MINUTE, t.PayDate,
                 CASE t.Status
                    WHEN 3 THEN GETDATE()
                    WHEN 4 THEN t.KeyRoomDate
                    When 5 THEN  t.KeyRoomDate
                    End) as KRRT,

                 datediff(MINUTE,t.PayDate,
                 CASE t.Status
                  WHEN 3 THEN GETDATE()
                  WHEN 4 THEN GETDATE()
                 WHEN 5 THEN t.DelDate
                 END) as DT

       FROM    
           dbo.Transaction_tbl t
       WHERE   

       ( 
           ([status] IN (3,4))
           OR 

           ([status]=5 and DelDate >=DATEADD(minute,-3,getdate()))
       )
       AND locid = @locid AND dtime >= @TodayMinus7Days
       ORDER BY  
           paydate 
    end

In Transaction_tbl i have a Index on locid,and status..and i am taking only last 7 days data from my table..i have more than 2 lack records in transaction table..in my stored procedure i given dtime >= @TodayMinus7Days .. dtime is the datetime insertion of each reocrds .i want to confirm becouse of this checking this will check only last 7 days data from my table? actually i want to check only last 7 days data from my transaction table..so i need to change anything in my stored procedure.i dont want to scan all my table..
instead of this AND locid = @locid AND dtime >= @TodayMinus7Days if i give like this AND locid = @locid AND dtime between DATEADD(DAY, -7, GetDate()) and GetDate() that will scan all table. .while executing this stored procedure i want to scan last 7 days records from my sql table,,how i can do this:?

除非我误解了这个问题,否则您可以再次使用DATEADD实现此目的:

 ...AND locid = @locid AND dtime between DATEADD(DAY, -7, GetDate()) and GetDate()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM