如何将存储过程中的动态参数声明为后面的代码

时间:2014-12-15 04:15:34

标签: sql .net sql-server vb.net

我有一个存储过程。存储过程中的参数是动态的。

我不知道在我页面的代码(VB.net)中声明和使用该参数。

这是存储过程:

CREATE PROCEDURE [dbo].[Gdata2]
    --@employeeID varchar(10), 
    --@employeeCostCenterCode varchar(20)
AS
BEGIN
    DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
    DECLARE @ColumnName AS NVARCHAR(MAX)

    --Get distinct values of the PIVOT Column 
    SELECT @ColumnName= ISNULL(@ColumnName + ',','') + QUOTENAME(Product)
    FROM (SELECT DISTINCT Product FROM V_SBR_Product) AS Products

    --Prepare the PIVOT query using the dynamic 
    SET @DynamicPivotQuery =  N'SELECT Quarter, ' + @ColumnName + 
                                'FROM V_SBR_Product ' +
                                'PIVOT(COUNT(Total) FOR Product IN (' + @ColumnName + 
                                ')) AS PVTTable'

    --Execute the Dynamic Pivot Query
    EXEC sp_executesql @DynamicPivotQuery
END

0 个答案:

没有答案