在SQL数据透视表结果中传递日期限制

时间:2016-03-14 21:18:50

标签: sql-server parameters pivot

此过程有效,直到我尝试传递参数以限制在数据透视表过程的主要选择查询中选择的记录。

如果我注释掉对@Startdate和@EndDate的引用,它可以工作,但是选择所有记录。 需要进行哪些更改,以便提示日期范围变量并将其合并到查询结果中?

我得到的错误是: Msg 137,Level 15,State 2,Line 10 必须声明标量变量" @ StartDate"。

这是下面的过程。

提前致谢。

ALTER PROCEDURE usp_Get_Monthly_IR_Report

@StartDate smalldatetime ,@ EndDate smalldatetime AS

DECLARE @ColumnNames NVARCHAR(MAX)=''

DECLARE @SQL NVARCHAR(Max)=''

SELECT @ColumnNames + = QUOTENAME(GL_Facilities_Name)+',' 来自GL_Facilities

SET @ColumnNames = LEFT(@ColumnNames,LEN(@ColumnNames)-1) SET @sql =

' SELECT * FROM(     SELECT IR_Priority_level.IR_Priority_Level_Text as [Priority],IR_Types.IR_Types_Text AS [Type],GL_Facilities.GL_Facilities_Name     FROM IR_Types LEFT JOIN IR_Priority_Level ON IR_Types.IR_Priority_Level_ID = IR_Priority_Level.IR_Priority_Level_ID     左外部联接IR_Incidents ON IR_Types.IR_Types_ID = IR_Incidents.IR_Types_ID     LEFT JOIN GL_Business_Line_Contracts ON IR_Incidents.GL_Business_Line_Contracts_ID = GL_Business_Line_Contracts.GL_Business_Line_Contracts_ID     LEFT JOIN GL_Facilities ON GL_Business_Line_Contracts.GL_Facilities_ID = GL_Facilities.GL_Facilities_ID WHERE IR_Incidents_Date @StartDate和@EndDate 之间     )AS BaseData

PIVOT( 计数(GL_Facilities_Name) FOR GL_Facilities_Name IN(' + @ ColumnNames +     ')     )作为数据透视表'

EXEC sp_executesql @SQL

1 个答案:

答案 0 :(得分:0)

因为您尝试在动态查询中使用过程参数,所以需要将它们传递给动态查询。

ALTER PROCEDURE usp_Get_Monthly_IR_Report
    @StartDate SMALLDATETIME,
    @EndDate SMALLDATETIME
AS 
BEGIN
    DECLARE @ColumnNames NVARCHAR(MAX) = ''
    DECLARE @SQL NVARCHAR(MAX) = ''

    -- define params that will be used in the dynamic query
    DECLARE @ParmDefinitions NVARCHAR(MAX) = '@StartDate SMALLDATETIME, @EndDate SMALLDATETIME'

    SELECT  @ColumnNames += QUOTENAME(GL_Facilities_Name) + ','
    FROM    GL_Facilities

    SET @ColumnNames = LEFT(@ColumnNames,LEN(@ColumnNames) - 1)
    SET @sql = 'SELECT * FROM ( 
                    SELECT  IR_Priority_level.IR_Priority_Level_Text as [Priority], 
                            IR_Types.IR_Types_Text AS [Type], 
                            GL_Facilities.GL_Facilities_Name 
                    FROM    IR_Types 
                            LEFT JOIN IR_Priority_Level ON IR_Types.IR_Priority_Level_ID = IR_Priority_Level.IR_Priority_Level_ID 
                            LEFT OUTER JOIN IR_Incidents ON IR_Types.IR_Types_ID = IR_Incidents.IR_Types_ID 
                            LEFT JOIN GL_Business_Line_Contracts ON IR_Incidents.GL_Business_Line_Contracts_ID = GL_Business_Line_Contracts.GL_Business_Line_Contracts_ID 
                            LEFT JOIN GL_Facilities ON GL_Business_Line_Contracts.GL_Facilities_ID = GL_Facilities.GL_Facilities_ID 
                    WHERE   IR_Incidents_Date BETWEEN @StartDate and @EndDate 
                ) AS BaseData
                PIVOT ( 
                    Count(GL_Facilities_Name) FOR GL_Facilities_Name IN (' + @ColumnNames + ') 
                ) AS PivotTable'

    EXEC sp_executesql @SQL, @ParmDefinitions, @StartDate = @StartDate, @EndDate = @EndDate
END