为动态数据透视查询创建存储过程

时间:2019-04-12 16:03:27

标签: sql-server tsql dynamic-sql

一段时间后,我成功地为数据库编写了动态数据透视查询。 现在,我需要将其转换为存储过程。我直接要求答案,因为我对存储过程的了解很少。 以下是我需要传递的代码和参数,它们分别位于('01 -01-2018'),('12-01-2018'),(``04 | 02''),(``FTE 1 '',``FTE2'',``FTE 3'')

DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX)
SELECT @cols = STUFF(
(
SELECT distinct ',' + QUOTENAME([MONTH]) 
FROM [MY_DB].[dbo].[ACTUAL_BILLABLE_HEADCOUNT] 
WHERE [MONTH] BETWEEN ('01-01-2018') AND ('12-01-2018')
ORDER BY ',' + QUOTENAME([MONTH]) 
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'),1,1,'') 

SET @query = 'SELECT ' + @cols + ' 
FROM 
(
SELECT [MONTH],[MONTH END BILLABLE] 
FROM [MY_DB].[dbo].[ACTUAL_BILLABLE_HEADCOUNT] 
WHERE [CC-LOC] = (''04|02'') AND [FTE/RATE CARD] IN (''FTE 1'',''FTE2'',''FTE 3'')
) x pivot (Sum ([MONTH END BILLABLE]) for [MONTH] in (' + @cols + '))p'
execute(@query)

1 个答案:

答案 0 :(得分:0)

将动态数据透视查询转换为存储过程不需要太多修改:)

关键在于定义变量以替换要传递到存储过程中的硬编码值。以下是一个有效的存储过程(名为usp_MonthEndBillableDynamicPivot):

CREATE PROCEDURE usp_MonthEndBillableDynamicPivot(
    @MonthFrom datetime,
    @MonthTo datetime,
    @CC_LOC nvarchar(10),
    @FTE_RATE_CARD nvarchar(100)
)
AS
DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX)
SELECT @cols = STUFF(
(
SELECT distinct ',' + QUOTENAME([MONTH]) 
FROM [MY_DB].[dbo].[ACTUAL_BILLABLE_HEADCOUNT] 
WHERE [MONTH] BETWEEN (@MonthFrom) AND (@MonthTo)
ORDER BY ',' + QUOTENAME([MONTH]) 
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'),1,1,'') 

SET @query = 'SELECT ' + @cols + ' 
FROM 
(
SELECT [MONTH],[MONTH END BILLABLE] 
FROM [MY_DB].[dbo].[ACTUAL_BILLABLE_HEADCOUNT] 
WHERE [CC-LOC] = (''' + @CC_LOC + ''') AND [FTE/RATE CARD] IN (' + @FTE_RATE_CARD + ')
) x pivot (Sum ([MONTH END BILLABLE]) for [MONTH] in (' + @cols + '))p'
execute(@query)

我必须对您实际的ACTUAL_BILLABLE_HEADCOUNT表中的数据类型做一些假设。为了进行测试,我在自己的测试数据库中创建了下表:

create table ACTUAL_BILLABLE_HEADCOUNT(
    ID int identity(1,1) not null,
    MONTH datetime,
    [MONTH END BILLABLE] money,
    ACTUAL_BILLABLE_HEADCOUNT int,
    [CC-LOC] nvarchar(10),
    [FTE/RATE CARD] nvarchar(100),
    constraint PK_ACTUAL_BILLABLE_HEADCOUNT primary key(ID)
)

并使用以下测试数据填充它(请注意,我使用的是YYYY-MM-DD ISO日期符号,以减少美国和英国日期(我的位置)之间的混淆)

insert into ACTUAL_BILLABLE_HEADCOUNT([MONTH],[MONTH END BILLABLE],ACTUAL_BILLABLE_HEADCOUNT,[CC-LOC],[FTE/RATE CARD])
values ('2018-01-01',10000.00,10,'04|02','FTE 1');

insert into ACTUAL_BILLABLE_HEADCOUNT([MONTH],[MONTH END BILLABLE],ACTUAL_BILLABLE_HEADCOUNT,[CC-LOC],[FTE/RATE CARD])
values ('2018-01-01',20000.00,20,'04|02','FTE 2');

insert into ACTUAL_BILLABLE_HEADCOUNT([MONTH],[MONTH END BILLABLE],ACTUAL_BILLABLE_HEADCOUNT,[CC-LOC],[FTE/RATE CARD])
values ('2018-01-01',30000.00,30,'04|02','FTE 3');

insert into ACTUAL_BILLABLE_HEADCOUNT([MONTH],[MONTH END BILLABLE],ACTUAL_BILLABLE_HEADCOUNT,[CC-LOC],[FTE/RATE CARD])
values ('2018-01-01',40000.00,40,'04|02','FTE 4');

insert into ACTUAL_BILLABLE_HEADCOUNT([MONTH],[MONTH END BILLABLE],ACTUAL_BILLABLE_HEADCOUNT,[CC-LOC],[FTE/RATE CARD])
values ('2018-02-01',10000.00,10,'04|02','FTE 1');

insert into ACTUAL_BILLABLE_HEADCOUNT([MONTH],[MONTH END BILLABLE],ACTUAL_BILLABLE_HEADCOUNT,[CC-LOC],[FTE/RATE CARD])
values ('2018-02-01',20000.00,20,'04|02','FTE 2');

insert into ACTUAL_BILLABLE_HEADCOUNT([MONTH],[MONTH END BILLABLE],ACTUAL_BILLABLE_HEADCOUNT,[CC-LOC],[FTE/RATE CARD])
values ('2018-02-01',30000.00,30,'04|02','FTE 3');

insert into ACTUAL_BILLABLE_HEADCOUNT([MONTH],[MONTH END BILLABLE],ACTUAL_BILLABLE_HEADCOUNT,[CC-LOC],[FTE/RATE CARD])
values ('2018-02-01',40000.00,40,'04|02','FTE 4');

请注意,我还包含一个附加的“ FTE 4”值,我用它来检查所有这些内容的动态性质!

通过以上所有操作,可以执行存储过程:

declare
    @MonthFrom datetime = '2018-01-01',
    @MonthTo datetime = '2018-12-01',
    @CC_LOC nvarchar(10) = '04|02',
    @FTE_RATE_CARD nvarchar(100) = '''FTE 1'',''FTE2'',''FTE 3'',''FTE 4'''
;

exec usp_MonthEndBillableDynamicPivot @MonthFrom, @MonthTo, @CC_LOC, @FTE_RATE_CARD;

具有以下结果:

Example Results

希望有帮助!