SSRS中存储过程中的多个数据集

时间:2014-07-18 16:49:58

标签: sql sql-server reporting-services dataset

我有一个返回四个不同结果集的SP,但是要在SSRS报告中使用它,我需要以正确的方式配置它。

USE LearnShare
GO

IF OBJECT_ID (N'dbo.JH_LearnShare_ActiveUsers', N'P') IS NOT NULL 
DROP PROCEDURE dbo.JH_LearnShare_ActiveUsers
GO
    CREATE PROCEDURE dbo.JH_LearnShare_ActiveUsers
        @enddate as date
    AS
    Begin
--Declare @enddate as date
--set @enddate = '6/30/2014'
    Select COUNT (distinct p.PersonID) as [Total Active Users] --50621
    FROM Enrollment E inner join 
    Person p on p.PersonID = e.[Person ID]
    WHERE p.active = 1
    and FirstLaunch between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,@enddate), 0)) AND @enddate
--DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,GETDATE()), 0)) AND GETDATE()
    and [Completion Date] between  DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,@enddate), 0))   AND @enddate
--and convert(varchar(10),[Enrollment Date] , 120) < convert(varchar(10),FirstLaunch , 120) --118669
--and FirstLaunch <> '1900-01-01 00:00:00.000' -- this doesn't matter when adding [completion date] in where clause


    Select COUNT (distinct p.PersonID) as [Total Active Online Users] --49269
    FROM Enrollment E inner join 
    Person p on p.PersonID = e.[Person ID]
    left join class c
    on e.[resource id] = c.[resource id] 
    WHERE p.active = 1
    and FirstLaunch between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,@enddate), 0)) AND @enddate
--DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,GETDATE()), 0)) AND GETDATE()
    and [Completion Date] between  DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,@enddate), 0)) AND @enddate
    and c.[Resource ID] is null 


    select COUNT (distinct p.personID) as [User with ILT courses] --22656
    from Person p 
    inner join Enrollment e on p.PersonID = e.[Person ID]
    inner join Resources r on e.[Resource ID] = r.[Resource ID]
    inner join Class c on r.[Resource ID] = c.[Resource ID]
    where p.Active = 1
    and c.[Class Date] between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,@enddate), 0)) AND @enddate
    and e.FirstLaunch between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,@enddate), 0)) AND @enddate
--DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,GETDATE()), 0)) AND GETDATE()
    and e.[Completion Date] between  DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,@enddate), 0)) AND @enddate


    SELECT DateName(yyyy, FirstLaunch) EnrollmentYear, DATENAME(month, FirstLaunch) AS EnrollmentMonth , Count(distinct p.PersonID) as ActiveUsers --190312
    FROM Enrollment E inner join 
    Person p on p.PersonID = e.[Person ID]
    WHERE p.active = 1
    and FirstLaunch between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,@enddate), 0)) AND @enddate
    and [Completion Date] between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,@enddate), 0)) AND @enddate
--and convert(varchar(10),[Enrollment Date] , 120) < convert(varchar(10),FirstLaunch , 120) --118669
--and FirstLaunch <> '1900-01-01 00:00:00.000' -- this doesn't matter when adding [completion date] in where clause 
    AND [Completion Date] <> '1900-01-01 00:00:00.000'
    GROUP BY DateName(yyyy,FirstLaunch) , DATENAME(month,FirstLaunch), DATEPART(MONTH,FirstLaunch)
    ORDER BY DateName(yyyy,FirstLaunch) desc , DATEPART(MONTH,FirstLaunch) DESC

    End

我试过了

Create StoredProcedure sp_MultipleDataSets(@Param nvarchar(10))
as
begin
   if(@Param == "first")
      begin
         SELECT EMPID, ENAME, JOB, SAL, DEPTID FROM EMP -- first result set
      end
   if(@Param == "second")
      begin
        SELECT DEPTID, DNAME, LOC FROM DEPT --second result set
      end
end

添加if(@enddate ==“first”)的示例开始选择.....结束但它给我一个错误 消息102,级别15,状态1,过程JH_LearnShare_ActiveUsers,第15行 '='附近的语法不正确。

请帮忙。谢谢!

1 个答案:

答案 0 :(得分:0)

我知道这已经晚了,但也许这个回复会帮助有人在以后找到这个。

可以以这种方式编写SP,但它似乎不必要地复杂化。应该没有理由不能只编写4个不同的存储过程。但是,如果你想沿着这条路走下去,那就行了。 EX:

IF @datasets = 1
begin
  select 1
end
IF @datasets = 2
begin
  select 2
end