带数组输入的SQL调用存储过程

时间:2018-01-17 18:54:35

标签: sql ms-access jet

背景

Piggybacking on a previous question我有一个包含这些样本值的表格:

Temp_Date             Building_ID          Sector_ID          Temperature    
[Date/Time]           [I32]                [I32]              [DBL]
1/9/2018 4:14:31 AM    456                   0                20.23    
1/9/2018 4:15:14 AM    123                   1                35.23    
1/9/2018 4:16:21 AM    123                   0                15.23    
1/9/2018 4:15:45 AM    123                   2                25.23    
1/9/2018 4:16:21 AM    456                   0                25.23    
1/9/2018 4:16:59 AM    123                   1                35.23

目前,我正在运行此SQL查询以获取每个扇区的最新值:

select 
    t.Building_ID, t.Sector_ID, t.Temperature, t.Temp_Date
from 
    MyTable t
inner join 
    (select 
         Building_ID, Sector_ID, max(date_time) as maxMaxTemp_Date
     from 
         MyTable t
     group by 
         Building_ID, Sector_ID) tm on t.Building_ID = tm.Building_ID 
                                    and t.Sector_ID = tm.Sector_ID 
                                    and t.Temp_Date = tm.MaxTemp_Date
order by
    t.Building_ID, t.Sector_ID

了解有关SQL的更多信息,我知道我可以:

  1. 添加WHERE Building_ID = [I32 value]以将返回的值限制为一个Building_ID值

  2. 将其转换为存储过程show-temps,使我的服务器能够使用语句`show-temps([value])更有效地返回所需的过滤数据。

  3. 做这两件事会让我看起来像

    CREATE PROCEDURE show-temps
        @Building int,
    AS
        SELECT 
            t.Building_ID, t.Sector_ID, t.Temperature, t.Temp_Date
        FROM 
            MyTable t
        INNER JOIN 
            (SELECT 
                 Building_ID, Sector_ID, MAX(date_time) AS maxMaxTemp_Date
             FROM
                 MyTable t
             GROUP BY 
                 Building_ID, Sector_ID) tm ON t.Building_ID = tm.Building_ID 
                                            AND t.Sector_ID = tm.Sector_ID 
                                            AND t.Temp_Date = tm.MaxTemp_Date
        WHERE 
            (t.Building_ID = @Building)        
        ORDER BY 
            t.Building_ID, t.Sector_ID
    

    我的问题:有没有办法将一组int值传递给WHERE()函数,以便我可以获取一组建筑物的最新数据而不是调用该函数多次?

0 个答案:

没有答案