简体   繁体   English

如何使用存储过程在sql server中检查上一周的数据与SQL表

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

my transaction_tbl structure like this 我的transaction_tbl结构是这样的

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? 在Transaction_tbl中,我有一个关于locid和status的索引..我仅从我的表中获取最近7天的数据..我在事务表中有2个以上的缺少记录..在我的存储过程中,我给出了dtime >= @TodayMinus7Days .. dtime是每个记录的日期时间插入。我想确认一下此检查,因为这只会检查我表中的最近7天数据吗? 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.. 实际上我只想检查交易表中最近7天的数据。所以我需要更改存储过程中的任何内容。我不想扫描我的所有表。
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. 代替这个AND locid = @locid AND dtime >= @TodayMinus7Days如果我这样给定AND locid = @locid AND dtime between DATEADD(DAY, -7, GetDate()) and GetDate()在将扫描所有表的AND locid = @locid AND dtime between DATEADD(DAY, -7, GetDate()) and GetDate() .while executing this stored procedure i want to scan last 7 days records from my sql table,,how i can do this:? 。在执行此存储过程时,我想从我的sql表中扫描最近7天的记录,我该怎么做:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用SQL Server从具有多个select语句的存储过程中获取一个数据表 - How to get one data table from Stored procedure that has multiple select statements using sql server SQL Server:在存储过程中使用表或@table - SQL Server: using table or @table in stored procedure 在SQL Server存储过程中使用检查约束 - Using check constraints in a SQL Server stored procedure 如何检查哪个存储过程/函数在SQL Server中使用时间戳将数据插入/删除/更新到特定表 - How to check which stored procedure/function inserts/deletes/updates data to particular table with time stamp in SQL server 在 SQL Server 2008 中使用 xml 和存储过程将数据插入到表中 - Insert data in to table using xml and stored procedure in SQL Server 2008 如何查看SQL服务器调用存储过程 - How to Check the calling stored procedure in SQL Server 如何在存储过程中使用循环将数据从一个表复制到SQL Server中的另一个表 - how to use loop in stored procedure to copy data from one table to another in sql server 如何将 sql 服务器存储过程数据插入到新表中? - how can insert sql server stored procedure data to the new table? 如何从SQL Server存储过程中读取数据表 - How to read data table from SQL Server stored procedure 在另一个存储过程中使用一个存储过程的结果-SQL Server - Using results of one stored procedure in another stored procedure - SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM