Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6180205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:48:21+00:00 2026-05-24T00:48:21+00:00

I need to accomplish the following in the stored procedure: Pass parameterized column names.

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:48:21+00:00Added an answer on May 24, 2026 at 12:48 am

    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:

    • sqlblog.org/dynamic-sql
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a class where I need accomplish the equivalent of the following
I need to write a WordPress query to accomplish the following - I am
I need to accomplish the following task: Attempt to move a file. If file
I need a solution that lets me accomplish the following: Returning CSS that is
I need a fairly complex regex to accomplish the following: > replace numbers in
Need help using regex and powershell to accomplish the following. I have the following
What settings do I need to accomplish the following? IIS6 Server sits on Domain1
I need to accomplish the following. 1.) I have a winform (winformA) that is
I'm scratching my head on how to accomplish the following task: I need to
I need to accomplish the same behavior as .NET Console.ReadLine function provides. The program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.