存储过程不显示结果

时间:2012-05-07 10:44:42

标签: sql-server sql-server-2008 stored-procedures reporting-services sql-server-2008-r2

我创建了一个存储过程(在SSRS中使用),其存储的参数值大于1。当我通过提供该参数的值在SSMS中执行此存储过程时,SP不会返回任何结果。它应该返回超过1行

以下是给出的SP

CREATE PROCEDURE [dbo].[sp_tst_CSENG_JulieCapitalHours]
        @StartDate DATETIME ,
        @EndDate DATETIME ,
        @ProjHomeGrp NVARCHAR(MAX) ,
        @ProjHier NVARCHAR(MAX)
AS  
BEGIN
    SELECT  [Capital Project] ,
                    [Capital Task] ,
                    ResourceName ,
                    ProjectName ,
                    [Project Home Group] ,
                    ActualWork ,
                    TimeByDay ,
                    ResourceStandardRate ,
                    ActualWork * ResourceStandardRate AS Dollars ,
                    [Project Hierarchy]
    FROM [IR.CapOnly]
    WHERE ( TimeByDay >= @StartDate )
      AND ( [Project Home Group] IN ( @ProjHomeGrp ) )
      AND ( TimeByDay <= @EndDate )
      AND ( ActualWork > 0 )
      AND ( [Project Hierarchy] IN ( @ProjHier ) )
    ORDER BY ProjectName ,
             ResourceName
END 

您可以在where子句中看到,Project Home Group(@ProjHomeGrp)和Project Hierarchy(@ProjHier)是参数,其提供的值(大于1)SP返回零值。

我传递的价值是:

Start Date: 1/1/2011 
End Date: 12/31/2012
@ProjHomeGrp : PHG1,PHG2,PHG3,PHG4,PHG5,PHG6,PHG7
@ProjHier: PROH1, PROH2, PROH3

让我知道任何问题!

3 个答案:

答案 0 :(得分:1)

这看起来不对:

[Project Home Group] IN ( @ProjHomeGrp )

您不能将INNVARCHAR类型一起使用,并希望能够直接对其使用基于集合的操作。

这可能更接近您所寻找的内容:

[Project Home Group] = @ProjHomeGrp

如果你使用IN参数作为分隔文本(比如使用逗号作为分隔符的整数),那么(在此猜测,使用NVARCHAR(MAX)),有更好的方法 - 你应该使用Table Valued Parameters就此而言。

答案 1 :(得分:0)

正如Oded所说,表值参数是要走的路。但是,这是一个基于动态sql的解决方案,其中包含可能意味着的所有问题......

create procedure [dbo].[sp_tst_CSENG_JulieCapitalHours]
        @StartDate datetime ,
        @enddate datetime ,
        @ProjHomeGrp nvarchar(MAX) ,
        @ProjHier nvarchar(MAX)
    as  
begin
    declare @sql nvarchar(max)
    declare @paramDefs nvarchar(max)

    select @sql = N'select  [Capital Project] ,
            [Capital Task] ,
            ResourceName ,
            ProjectName ,
            [Project Home Group] ,
            ActualWork ,
            TimeByDay ,
            ResourceStandardRate ,
            ActualWork * ResourceStandardRate AS Dollars ,
            [Project Hierarchy]
    from    [IR.CapOnly]
    where   ( TimeByDay >= @StartDate )
            and ( [Project Home Group] IN (' + @ProjHomeGrp + ') )
            and ( TimeByDay <= @EndDate) )
            and ( ActualWork > 0 )
            and ( [Project Hierarchy] IN ( ' + @ProjHier + ' ) )
    order by ProjectName, ResourceName'

    select @paramDefs = N'@StartDate datetime, @EndDate datetime'

    exec sp_executesql @sql, @paramDefs, @StartDate = @StartDate, @EndDate =  @EndDate
end 

答案 2 :(得分:0)

创建如下的程序:

CREATE PROCEDURE Show_Sproceduresome
   AS 
 Begin
    select points,coursename,c.type from OfferType o,Courses c,Candidate cd, CourseOffer
     where name = 'Francisco' 
     and o.OfferId = co.OfferId 
     and co.CourseId = c.CourseId 
     and co.UCASNumber = cd.UCASNumber
 End

执行以下程序:

exec Sproceduresome

不显示结果或输出