列名由参数决定
Column name determined by parameter
两种方法我都试过了。第一个有语法错误。第二个推送结果中的每一列,而不仅仅是与@LabelID 匹配的那一列。
SELECT (SELECT CASE @LabelID
WHEN 1 THEN count(h.ee_cmn_idfr) as 'DIR'
WHEN 2 THEN count(h.ee_cmn_idfr) as 'DD'
WHEN 3 THEN count(h.ee_cmn_idfr) as 'OD_Staff'
WHEN 4 THEN count(h.ee_cmn_idfr) as 'DI_BC'
WHEN 5 THEN count(h.ee_cmn_idfr) as 'DI_PLs'
WHEN 6 THEN count(h.ee_cmn_idfr) as 'DI_PQAs'
WHEN 7 THEN count(h.ee_cmn_idfr) as 'DI_FTEs'
WHEN 8 THEN count(h.ee_cmn_idfr) as 'AIPQBBC'
WHEN 9 THEN count(h.ee_cmn_idfr) as 'AIPQB_PL'
WHEN 10 THEN count(h.ee_cmn_idfr) as 'AIPQB_PQA'
WHEN 11 THEN count(h.ee_cmn_idfr) as 'AIPQB_GS13S'
WHEN 12 THEN count(h.ee_cmn_idfr) as 'AIPQB_FTE'
WHEN 13 THEN count(h.ee_cmn_idfr) as 'IT_Staff'
WHEN 14 THEN count(h.ee_cmn_idfr) as 'IT_Sup'
)
第二种方法:
SELECT
CASE WHEN @LabelID = 1 THEN count(h.ee_cmn_idfr) as 'DIR'
,CASE WHEN @LabelID = 2 THEN count(h.ee_cmn_idfr) END as 'DD'
,CASE WHEN @LabelID = 3 THEN count(h.ee_cmn_idfr) END as 'OD_Staff'
,CASE WHEN @LabelID = 4 THEN count(h.ee_cmn_idfr) END as 'DI_BC'
,CASE WHEN @LabelID = 5 THEN count(h.ee_cmn_idfr) END as 'DI_PLs'
,CASE WHEN @LabelID = 6 THEN count(h.ee_cmn_idfr) END as 'DI_PQAs'
,CASE WHEN @LabelID = 7 THEN count(h.ee_cmn_idfr) END as 'DI_FTEs'
,CASE WHEN @LabelID = 8 THEN count(h.ee_cmn_idfr) END as 'AIPQBBC'
,CASE WHEN @LabelID = 9 THEN count(h.ee_cmn_idfr) END as 'AIPQB_PL'
,CASE WHEN @LabelID = 10 THEN count(h.ee_cmn_idfr) END as 'AIPQB_PQA'
,CASE WHEN @LabelID = 11 THEN count(h.ee_cmn_idfr) END as 'AIPQB_GS13S'
,CASE WHEN @LabelID = 12 THEN count(h.ee_cmn_idfr) END as 'AIPQB_FTE'
,CASE WHEN @LabelID = 13 THEN count(h.ee_cmn_idfr) END as 'IT_Staff'
,CASE WHEN @LabelID = 14 THEN count(h.ee_cmn_idfr) END as 'IT_Sup'
这是使用动态 SQL:
的方法
示例数据:
IF OBJECT_ID('tempdb..#INPUT') IS NOT NULL
DROP TABLE #INPUT;
CREATE TABLE #INPUT(RowID INT IDENTITY(1, 1)
, ee_cmn_idfr INT);
INSERT INTO #INPUT(ee_cmn_idfr)
VALUES
(1),
(1),
(1),
(1),
(1),
(1),
(1),
(1),
(1),
(1),
(1),
(1);
SQL 查询:
DECLARE @LabelID INT = 1; --<-- set the labelID
DECLARE @SQL NVARCHAR(MAX) = ''; --<-- declare a variable to hold the dynamic sql
SELECT @SQL = 'SELECT COUNT(ee_cmn_idfr) AS '+QUOTENAME(CASE @LabelID
WHEN 1 THEN 'DIR'
WHEN 2 THEN 'DD'
WHEN 3 THEN 'OD_Staff'
WHEN 4 THEN 'DI_BC'
WHEN 5 THEN 'DI_PLs'
WHEN 6 THEN 'DI_PQAs'
WHEN 7 THEN 'DI_FTEs'
WHEN 8 THEN 'AIPQBBC'
WHEN 9 THEN 'AIPQB_PL'
WHEN 10 THEN 'AIPQB_PQA'
WHEN 11 THEN 'AIPQB_GS13S'
WHEN 12 THEN 'AIPQB_FTE'
WHEN 13 THEN 'IT_Staff'
WHEN 14 THEN 'IT_Sup'
END)+' FROM #INPUT'; --<-- you'll have to change the name of the table accordingly
PRINT(@SQL); --<-- print out the query not needed but nice to have for debuging
EXEC (@SQL); --<-- execute the dynamic sql
PRINT(@SQL)
将打印以下内容:
SELECT COUNT(ee_cmn_idfr) AS [DIR] FROM #INPUT
结果:
以上代码将select列名如下:
- 当 1 那么 'DIR'
- 当 2 然后 'DD'
- 当 3 然后 'OD_Staff'
- 当 4 那么 'DI_BC'
- 当 5 时 'DI_PLs'
- 当 6 那么 'DI_PQAs'
- 当 7 然后 'DI_FTEs'
- 当 8 那么 'AIPQBBC'
- 当 9 然后 'AIPQB_PL'
- 当 10 时 'AIPQB_PQA'
- 当 11 时 'AIPQB_GS13S'
- 当 12 时 'AIPQB_FTE'
- 当 13 时 'IT_Staff'
- 当 14 时 'IT_Sup'
两种方法我都试过了。第一个有语法错误。第二个推送结果中的每一列,而不仅仅是与@LabelID 匹配的那一列。
SELECT (SELECT CASE @LabelID
WHEN 1 THEN count(h.ee_cmn_idfr) as 'DIR'
WHEN 2 THEN count(h.ee_cmn_idfr) as 'DD'
WHEN 3 THEN count(h.ee_cmn_idfr) as 'OD_Staff'
WHEN 4 THEN count(h.ee_cmn_idfr) as 'DI_BC'
WHEN 5 THEN count(h.ee_cmn_idfr) as 'DI_PLs'
WHEN 6 THEN count(h.ee_cmn_idfr) as 'DI_PQAs'
WHEN 7 THEN count(h.ee_cmn_idfr) as 'DI_FTEs'
WHEN 8 THEN count(h.ee_cmn_idfr) as 'AIPQBBC'
WHEN 9 THEN count(h.ee_cmn_idfr) as 'AIPQB_PL'
WHEN 10 THEN count(h.ee_cmn_idfr) as 'AIPQB_PQA'
WHEN 11 THEN count(h.ee_cmn_idfr) as 'AIPQB_GS13S'
WHEN 12 THEN count(h.ee_cmn_idfr) as 'AIPQB_FTE'
WHEN 13 THEN count(h.ee_cmn_idfr) as 'IT_Staff'
WHEN 14 THEN count(h.ee_cmn_idfr) as 'IT_Sup'
)
第二种方法:
SELECT
CASE WHEN @LabelID = 1 THEN count(h.ee_cmn_idfr) as 'DIR'
,CASE WHEN @LabelID = 2 THEN count(h.ee_cmn_idfr) END as 'DD'
,CASE WHEN @LabelID = 3 THEN count(h.ee_cmn_idfr) END as 'OD_Staff'
,CASE WHEN @LabelID = 4 THEN count(h.ee_cmn_idfr) END as 'DI_BC'
,CASE WHEN @LabelID = 5 THEN count(h.ee_cmn_idfr) END as 'DI_PLs'
,CASE WHEN @LabelID = 6 THEN count(h.ee_cmn_idfr) END as 'DI_PQAs'
,CASE WHEN @LabelID = 7 THEN count(h.ee_cmn_idfr) END as 'DI_FTEs'
,CASE WHEN @LabelID = 8 THEN count(h.ee_cmn_idfr) END as 'AIPQBBC'
,CASE WHEN @LabelID = 9 THEN count(h.ee_cmn_idfr) END as 'AIPQB_PL'
,CASE WHEN @LabelID = 10 THEN count(h.ee_cmn_idfr) END as 'AIPQB_PQA'
,CASE WHEN @LabelID = 11 THEN count(h.ee_cmn_idfr) END as 'AIPQB_GS13S'
,CASE WHEN @LabelID = 12 THEN count(h.ee_cmn_idfr) END as 'AIPQB_FTE'
,CASE WHEN @LabelID = 13 THEN count(h.ee_cmn_idfr) END as 'IT_Staff'
,CASE WHEN @LabelID = 14 THEN count(h.ee_cmn_idfr) END as 'IT_Sup'
这是使用动态 SQL:
的方法示例数据:
IF OBJECT_ID('tempdb..#INPUT') IS NOT NULL
DROP TABLE #INPUT;
CREATE TABLE #INPUT(RowID INT IDENTITY(1, 1)
, ee_cmn_idfr INT);
INSERT INTO #INPUT(ee_cmn_idfr)
VALUES
(1),
(1),
(1),
(1),
(1),
(1),
(1),
(1),
(1),
(1),
(1),
(1);
SQL 查询:
DECLARE @LabelID INT = 1; --<-- set the labelID
DECLARE @SQL NVARCHAR(MAX) = ''; --<-- declare a variable to hold the dynamic sql
SELECT @SQL = 'SELECT COUNT(ee_cmn_idfr) AS '+QUOTENAME(CASE @LabelID
WHEN 1 THEN 'DIR'
WHEN 2 THEN 'DD'
WHEN 3 THEN 'OD_Staff'
WHEN 4 THEN 'DI_BC'
WHEN 5 THEN 'DI_PLs'
WHEN 6 THEN 'DI_PQAs'
WHEN 7 THEN 'DI_FTEs'
WHEN 8 THEN 'AIPQBBC'
WHEN 9 THEN 'AIPQB_PL'
WHEN 10 THEN 'AIPQB_PQA'
WHEN 11 THEN 'AIPQB_GS13S'
WHEN 12 THEN 'AIPQB_FTE'
WHEN 13 THEN 'IT_Staff'
WHEN 14 THEN 'IT_Sup'
END)+' FROM #INPUT'; --<-- you'll have to change the name of the table accordingly
PRINT(@SQL); --<-- print out the query not needed but nice to have for debuging
EXEC (@SQL); --<-- execute the dynamic sql
PRINT(@SQL)
将打印以下内容:
SELECT COUNT(ee_cmn_idfr) AS [DIR] FROM #INPUT
结果:
以上代码将select列名如下:
- 当 1 那么 'DIR'
- 当 2 然后 'DD'
- 当 3 然后 'OD_Staff'
- 当 4 那么 'DI_BC'
- 当 5 时 'DI_PLs'
- 当 6 那么 'DI_PQAs'
- 当 7 然后 'DI_FTEs'
- 当 8 那么 'AIPQBBC'
- 当 9 然后 'AIPQB_PL'
- 当 10 时 'AIPQB_PQA'
- 当 11 时 'AIPQB_GS13S'
- 当 12 时 'AIPQB_FTE'
- 当 13 时 'IT_Staff'
- 当 14 时 'IT_Sup'