如何通过更改参数使此存储过程重复?

时间:2014-11-12 21:53:54

标签: sql-server reporting-services sql-server-2000

我在SSRS中使用此存储过程作为报告查询。 我想在本月的每一天都重复这一点,这样我就可以输出整整几个月的时间。表中的日期最小/最大时间。 参数在SSRS中设置,我创建了一个带有日期编号的单独表。 当我在SSRS中使用多项选择时,它给了我一个转换错误。 (DATEPART(dd, Events.FieldTime) = @date想要一个INT,但是逗号似乎弄得一团糟)我该怎么用?提示?想法?

@userID VARCHAR(200), @date NVARCHAR(50), @month INT AS
--Main SELECT clause that displays users and their entry/exit times from departments 3, 7, 10, 14, 15
SELECT DISTINCT TOP 10000
       [PTUsers].[Name]

      --subquery that finds the min time on the selected door on the given date
      ,(SELECT TOP 1
        MIN([Events].[FieldTime]) AS MinEntry

        FROM
        [Events], [PTDoors] pd, [PTUsers] ptu
        INNER JOIN
        [PTUsers] ON [Events].[GlobalIndex1] = [ptu].[GlobalRecord]

        WHERE [Events].[GlobalIndex1] = [ptu].[GlobalRecord]
        AND   [Events].[RecordIndex2] = 14
        --AND ptu.Name = PTUsers.Name
        AND (DATEPART(dd, [Events].[FieldTime]) = @date)
        AND (DATEPART(mm, [Events].[FieldTime]) = @month)
        AND (DATEPART(yyyy, [Events].[FieldTime]) = DATEPART(year, GETDATE()) )
        --AND (pd.Panel = 0)
        --AND (PTUsers.Panel = 0)

        GROUP BY
        [PTUsers].[Name]
        ) AS MinTime

        --subquery that finds the min time on the selected door one day prior to the given date
      ,(SELECT TOP 1
        MIN([Events].[FieldTime]) AS MinEntryYest

        FROM
        [Events], [PTDoors] pd, [PTUsers] ptu
        INNER JOIN
        [PTUsers] ON [Events].[GlobalIndex1] = [ptu].[GlobalRecord]

        WHERE 
            [Events].[GlobalIndex1] = [ptu].[GlobalRecord]
        AND [Events].[RecordIndex2] = 14
        --AND ptu.Name = PTUsers.Name
        AND (DATEPART(dd, [Events].[FieldTime]) =@date-1)
        AND (DATEPART(mm, [Events].[FieldTime]) = @month)
        AND (DATEPART(yyyy, [Events].[FieldTime]) = DATEPART(year, GETDATE()) )
        AND (DATEPART(HH, [Events].[FieldTime]) >= 21 )
        --AND (pd.Panel = 0)
        --AND (PTUsers.Panel = 0)

        GROUP BY
        [PTUsers].[Name]
        ) AS MinTimeYest

        --subquery that finds the max time on the selected door on the given date
        ,(SELECT TOP 1
        MAX([Events].[FieldTime]) AS MaxEntry

        FROM
        [Events], [PTDoors] pd1, [PTUsers] ptu 
        INNER JOIN
        [PTUsers] ON [Events].[GlobalIndex1] = [ptu].[GlobalRecord]

        WHERE [Events].[GlobalIndex1] = [ptu].[GlobalRecord]
        AND   [Events].[RecordIndex2] = 15
        --AND ptu.Name = PTUsers.Name
        AND (DATEPART(dd, [Events].[FieldTime]) = @date)
        AND (DATEPART(mm, [Events].[FieldTime]) = @month)
        AND (DATEPART(yyyy, [Events].[FieldTime]) = DATEPART(year, GETDATE()) )
        --AND (pd1.Panel = 0)
        --AND (PTUsers.Panel = 0)


        GROUP BY
        [PTUsers].[Name]
        ) AS MaxTime


  FROM [dbo].[DateTbl], [dbo].[Events]
  INNER JOIN
  [dbo].[PTUsers] ON PTUsers.GlobalRecord = [Events].[GlobalIndex1]
  INNER JOIN
  [dbo].[PTDoors] ON [Events].[RecordIndex2] = PTDoors.PTDoorsID

WHERE
        (PTUsers.Panel = 0) 
    AND (PTDoors.Panel = 0) 
    AND (PTDoors.PTDoorsID = 14 OR PTDoors.PTDoorsID = 15)
    AND (PTUsers.AccessLevel IN (3,7,10,14,15))
    AND (DATEPART(month, [Events].[FieldTime]) = (@month))
    AND (PTUsers.GlobalRecord) IN (SELECT Item FROM dbo.SplitUser(@userID, ','))
    AND (DateTbl.DateNR) IN (SELECT Item FROM dbo.SplitDates(@date, ','))

ORDER BY MaxTime DESC 

1 个答案:

答案 0 :(得分:0)

您可以使用返回TABLE的拆分函数(TSQL或CLR)。然后,您可以加入该结果集。在这里,您可以找到如何在TSQL中实现它:http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/