简体   繁体   English

通过SQL Server中的指定参数动态选择和分组

[英]Dynamically selecting and grouping by specified parameters in SQL Server

I am trying to create optional parameters in a stored process in which I group by the parameters under certain conditions. 我试图在存储过程中创建可选参数,在某些情况下,我将这些参数按参数分组。

For example: 例如:

SELECT
    TP.ProductID,
    case 
        when @passangers='Y'     then (TP.Passangersgroup)
        when @fareclass='Y'      then (TP.Fareclass)
        when @ispriorbooking='Y' then (TP.IsPriorBooking)
    end
INTO ##B
FROM ##A TP
GROUP BY  
    TP.ProductID, 
    case 
        when @passangers='Y'     then (TP.Passangersgroup)
        when @fareclass='Y'      then (TP.Fareclass)
        when @ispriorbooking='Y' then (TP.IsPriorBooking)
    end

In this case, I would be able to select 'Y' for any of the 3 parameters, and I would want to add them to select statement and group by. 在这种情况下,我可以为3个参数中的任何一个选择'Y',并且我想将它们添加到select语句和group by中。

Any ideas? 有任何想法吗?

You need to do this with dynamic SQL; 您需要使用动态SQL进行此操作。 something like: 就像是:

declare @sql varchar(max) = 'SELECT
    TP.ProductID, ' + 
    case when @passangers='Y' then 'TP.Passangersgroup'
    when @fareclass='Y' then 'TP.Fareclass'
    when @ispriorbooking='Y' then 'TP.IsPriorBooking'
else ''        
end
+ ' INTO ##B
FROM ##A TP'
--ETC

Exec(@sql)

If you want to add up to all three columns, you need three case statements: 如果您希望将所有三列加起来,则需要三个case语句:

declare @sql varchar(max) = 'SELECT
    TP.ProductID, ' + 
    case when @passangers='Y' then 'TP.Passangersgroup' else '' end
    + case when @fareclass='Y' then 'TP.Fareclass' else '' end
--ETC.
+ ' INTO ##B
FROM ##A TP'

Dynamic SQL will be the best bet, but I would figure out the column you want and then pass in the one column as a variable. 动态SQL将是最好的选择,但是我会找出所需的列,然后将一列作为变量传递。 Less likely to suffer SQL injection and more readable. 受SQL注入的可能性较小,并且更具可读性。

DECLARE @passangers CHAR(1), @fareclass CHAR(1), @ispriorbooking CHAR(1)
SET @passangers='Y'

DECLARE @SQLCMD NVARCHAR(MAX), @YValue NVARCHAR(1000)

--set the select and group by field
SELECT @YValue=
    case 
        when @passangers='Y' then N'TP.Passangersgroup'
        when @fareclass='Y' then N'TP.Fareclass'
        when @ispriorbooking='Y' then N'TP.IsPriorBooking'
        else NULL
    end

IF @YValue IS NOT NULL
BEGIN
    SET @SQLCMD=N'
    SELECT
    TP.ProductID,'+@YValue+'
    INTO ##B
    FROM ##A TP
    GROUP BY  
        TP.ProductID, '+@YValue

    PRINT @SQLCMD
    --EXEC sp_executesql @SQLCMD
END
ELSE
    PRINT 'INVALID PARAMETER PASSED IN'

You have to use dynamic sql but case statement mentioned in Steve Mangiameli code will not work in case when more than one column is selected as 'Y'. 您必须使用动态sql,但如果选择多个列作为“ Y”,则Steve Mangiameli代码中提到的case语句将不起作用。 The below code will be working fine for multiple columns selected as 'Y'- 以下代码对于选择为“ Y”的多个列将正常工作-

create procedure proc1
@passangers varchar(100) = null,
@fareclass varchar(100) = null,
@ispriorbooking varchar(100) = null
as
begin
declare @sql nvarchar(100)
declare @var varchar(100)

if @passangers = 'y'
set @var = tp.Passangersgroup + ', '
if @fareclass = 'y'
set @var = @var + TP.Fareclass + ', '
if @ispriorbooking = 'y'
set @var = @var + TP.IsPriorBooking


set @sql =  'select ' + @var + ' into ##b from ##a as TP group by  ' + @var + 'option(recomplile)'

exec sp_executesql @sql
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM