使用动态列名称创建SQL存储过程

时间:2019-01-11 14:53:05

标签: sql-server tsql stored-procedures

我有以下存储过程:

ALTER PROCEDURE [dbo].[sp_RequestCategoryCount]
    @StDate1 DATE,
    @EnDate1 DATE,
    @StDate2 DATE,
    @EnDate2 DATE
AS
BEGIN
    SELECT DISTINCT RQ.request_category
    INTO #ReqCat
    FROM 
        (SELECT DISTINCT request_category 
         FROM [VerInt_OneViewServiceReq]
         UNION ALL
         SELECT DISTINCT request_category 
         FROM [VerInt_OneViewServiceReq2]) RQ

    SELECT
        '1' as Iteration,
        request_category,
        SUM(1) AS Record_Count
    INTO 
        #Iter1
    FROM
        [VerInt_OneViewServiceReq]
    WHERE
        request_created_dt BETWEEN @StDate1 AND @EnDate1
    GROUP BY 
        request_category

    -- UNION ALL

    SELECT 
        '2' as Iteration,
        request_category,
        SUM(1) as Record_Count
    INTO 
        #Iter2
    FROM
        [VerInt_OneViewServiceReq2]
    WHERE
        request_created_dt BETWEEN @StDate2 AND @EnDate2
    GROUP BY
        request_category

    -- ORDER BY Iteration, request_category ASC

    SELECT 
        RC.request_category, 
        IT1.Record_Count as RecordCountDateRange1,
        IT2.Record_Count as RecordCountDateRange2
    FROM
        #ReqCat RC
    LEFT JOIN 
        #Iter1 IT1 ON IT1.request_category = RC.request_category
    LEFT JOIN 
        #Iter2 IT2 ON IT2.request_category = RC.request_category

    DROP TABLE #ReqCat
    DROP TABLE #Iter1
    DROP TABLE #Iter2
END

它所做的基本上是通过并排排列两个表来比较两个表中的两个数据跨度,并且工作正常。

我想做的是替换最后一个SELECT语句中的内容:

SELECT
    RC.request_category, 
    IT1.Record_Count as RecordCountDateRange1,
    IT2.Record_Count as RecordCountDateRange2
FROM
    #ReqCat RC

带有类似“空中代码”的内容:

SELECT
    RC.request_category, 
    IT1.Record_Count AS "@StDate1_to_@EnDate1",
    IT2.Record_Count AS "@StDate2_to_@EnDate2"
FROM
    #ReqCat RC

,以便字段名称指示比较中使用的日期。

是否可以在不重写整个过程的情况下进行操作?如果可以,我将如何做?

1 个答案:

答案 0 :(得分:3)

是的,这可以通过使用动态SQL来实现。我无法对其进行测试,但是或多或少它应该可以按照您指定的方式工作。它会在屏幕上(消息区域)显示正在运行的句子,您可以根据需要调整SELECT @SQL = ...,直到找到正确的语句:

ALTER procedure [dbo].[sp_RequestCategoryCount]
    @StDate1 date,
    @EnDate1 date,
    @StDate2 date,
    @EnDate2 date
AS

BEGIN
DECLARE @SQL AS NVARCHAR(MAX);

SELECT distinct RQ.request_category
into #ReqCat
FROM ( Select distinct request_category from [VerInt_OneViewServiceReq]
         UNION ALL
       Select distinct request_category from [VerInt_OneViewServiceReq2]
       ) RQ


Select 
'1' as Iteration,
request_category,
Sum(1) as Record_Count
INTO #Iter1
from [VerInt_OneViewServiceReq]
where request_created_dt between @StDate1 and @EnDate1
group by request_category

--UNION ALL

Select 
'2' as Iteration,
request_category,
Sum(1) as Record_Count
INTO #Iter2
from [VerInt_OneViewServiceReq2]
where request_created_dt between @StDate2 and @EnDate2
group by request_category

--order by Iteration, request_category ASC

SELECT @SQL = '
Select RC.request_category, 
   IT1.Record_Count as ['+CONVERT(VARCHAR(10), @StDate1)+'_to_'+CONVERT(VARCHAR(10), @EnDate1)+'],
   IT2.Record_Count as ['+CONVERT(VARCHAR(10), @StDate2)+'_to_'+CONVERT(VARCHAR(10), @EnDate2)+']
from #ReqCat RC
LEFT JOIN #Iter1 IT1
  ON IT1.request_category = RC.request_category
LEFT JOIN #Iter2 IT2
  ON IT2.request_category = RC.request_category'
-- Debug purposes
PRINT @SQL;
EXEC sp_ExecuteSQL @SQL;

Drop Table #ReqCat
Drop Table #Iter1
Drop Table #Iter2

END