SELECT 动态列 GROUP BY 动态列

发布于 2024-11-26 15:42:45 字数 993 浏览 0 评论 0原文

我需要在存储过程中完成以下操作:

  1. 传递参数化列名称。
  2. 选择参数化列名称并按所选列提供总组。

代码:

CREATE PROCEDURE sproc (
  @column1 NVARCHAR(MAX),
  @column2 NVARCHAR(MAX),
  @startdate DATE,
  @enddate DATE ) AS

BEGIN

  DECLARE @sqlquery NVARCHAR(MAX) = 'SELECT @column1, @column2, SUM(amountcolumn)
                                       FROM tablename
                                      WHERE column3 = ''@value3'',
                                            datecolumn BETWEEN ''@startdate'' AND ''@enddate''
                                   GROUP BY @column1, @column2';

  DECLARE @params NVARCHAR(MAX) = '@column1 VARCHAR(MAX),
                                   @column2 VARCHAR(MAX),
                                   @startdate DATE,
                                   @enddate  DATE';

  EXEC sp_sqlexec @sqlquery, @params,
       @column1 = @column1,
       @column2 = @column2,
       @startdate = @startdate,
       @enddate = @enddate;

END

GO

I need to accomplish the following in the stored procedure:

  1. Pass parameterized column names.
  2. Select the parameterized column names and provide total group by selected columns.

Code:

CREATE PROCEDURE sproc (
  @column1 NVARCHAR(MAX),
  @column2 NVARCHAR(MAX),
  @startdate DATE,
  @enddate DATE ) AS

BEGIN

  DECLARE @sqlquery NVARCHAR(MAX) = 'SELECT @column1, @column2, SUM(amountcolumn)
                                       FROM tablename
                                      WHERE column3 = ''@value3'',
                                            datecolumn BETWEEN ''@startdate'' AND ''@enddate''
                                   GROUP BY @column1, @column2';

  DECLARE @params NVARCHAR(MAX) = '@column1 VARCHAR(MAX),
                                   @column2 VARCHAR(MAX),
                                   @startdate DATE,
                                   @enddate  DATE';

  EXEC sp_sqlexec @sqlquery, @params,
       @column1 = @column1,
       @column2 = @column2,
       @startdate = @startdate,
       @enddate = @enddate;

END

GO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2024-12-03 15:42:45

假设 @value3 是一个字符串,并且是存储过程的另一个参数,那么 datecolumn 实际上是 date,并忽略我有的事实不知道如何拥有一个模式,其中分组字段可以像这样随机(您在此处忽略了其他最近的问题):

DECLARE @sql nvarchar(max) = N'SELECT ' 
      + QUOTENAME(@column1) + ', ' 
      + QUOTENAME(@column2) + ', SUM(amountcolumn)
    FROM dbo.tablename
    WHERE column3 = @value3
    AND datecolumn BETWEEN @startdate AND @enddate
    GROUP BY ' + QUOTENAME(@column1) 
        + ', ' + QUOTENAME(@column2) + ';';

EXEC sys.sp_executesql @sql,
  N'@value3 varchar(255), @startdate date, @enddate date',
    @value3, @startdate, @enddate; 
    -- strongly recommend against sp_sqlexec
    -- it is undocumented and unsupported

这也假设您不关心顺序(您可能会并且想要添加 ORDER BY 以及如GROUP BY)。

有关动态 SQL 的更多信息,以及保护自己免受用户输入影响的进一步方法:

Assuming @value3 is a string and is another parameter to the stored procedure, that datecolumn is in fact date, and ignoring the fact that I have no idea how you can have a schema where the grouping fields can be random like this (which you ignored in other recent questions here):

DECLARE @sql nvarchar(max) = N'SELECT ' 
      + QUOTENAME(@column1) + ', ' 
      + QUOTENAME(@column2) + ', SUM(amountcolumn)
    FROM dbo.tablename
    WHERE column3 = @value3
    AND datecolumn BETWEEN @startdate AND @enddate
    GROUP BY ' + QUOTENAME(@column1) 
        + ', ' + QUOTENAME(@column2) + ';';

EXEC sys.sp_executesql @sql,
  N'@value3 varchar(255), @startdate date, @enddate date',
    @value3, @startdate, @enddate; 
    -- strongly recommend against sp_sqlexec
    -- it is undocumented and unsupported

This also assumes you don't care about order (you probably do and will want to add ORDER BY as well as GROUP BY).

For more info on dynamic SQL and even further ways to protect yourself from user input:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文