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 1115521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:10:05+00:00 2026-05-17T03:10:05+00:00

I was wondering if there was a way to do this in MS SQL

  • 0

I was wondering if there was a way to do this in MS SQL Server 2005:

  DECLARE @theDate varchar(60)
  SET @theDate = '''2010-01-01'' AND ''2010-08-31 23:59:59'''

  SELECT    AdministratorCode, 
            SUM(Total) as theTotal, 
            SUM(WOD.Quantity) as theQty, 
            AVG(Total) as avgTotal, 
            (SELECT SUM(tblWOD.Amount)
                FROM tblWOD
                JOIN tblWO on tblWOD.OrderID = tblWO.ID
                WHERE tblWO.Approved = '1' 
                AND tblWO.AdministratorCode = tblWO.AdministratorCode
                AND tblWO.OrderDate BETWEEN @theDate
            )
 ... etc

Is this possible to do?

  • 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-17T03:10:06+00:00Added an answer on May 17, 2026 at 3:10 am

    It’s possible, but it requires using dynamic SQL.
    I recommend reading The curse and blessings of dynamic SQL before continuing…

    DECLARE @theDate varchar(60)
    SET @theDate = '''2010-01-01'' AND ''2010-08-31 23:59:59'''
    
    DECLARE @SQL VARCHAR(MAX)  
    SET @SQL = 'SELECT AdministratorCode, 
                       SUM(Total) as theTotal, 
                       SUM(WOD.Quantity) as theQty, 
                       AVG(Total) as avgTotal, 
                      (SELECT SUM(tblWOD.Amount)
                         FROM tblWOD
                         JOIN tblWO on tblWOD.OrderID = tblWO.ID
                        WHERE tblWO.Approved = ''1''
                          AND tblWO.AdministratorCode = tblWO.AdministratorCode
                          AND tblWO.OrderDate BETWEEN '+ @theDate +')'
    
    EXEC(@SQL)
    

    Dynamic SQL is just a SQL statement, composed as a string before being executed. So the usual string concatenation occurs. Dynamic SQL is required whenever you want to do something in SQL syntax that isn’t allowed, like:

    • a single parameter to represent comma separated list of values for an IN clause
    • a variable to represent both value and SQL syntax (IE: the example you provided)

    EXEC sp_executesql allows you to use bind/preparedstatement parameters so you don’t have to concern yourself with escaping single quotes/etc for SQL injection attacks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just wondering if there's a better way of doing this in SQL Server
I was wondering if there was a way to accomplish this, or if there
I was wondering if there is a way to simplify this down from two
I am wondering if there is a way to do this using Core Animation.
I am wondering if there is a way to handle this more elegantly. After
I am wondering if there is a better way to write this JavaScript (Jquery)
I'm wondering if there is a more elegant way to do this: $foo =
I'm wondering if there's a shorter way to write this: var x = 1;
If I have this line and I'm wondering if there's better way to do
I was wondering if there is a way, using PHP, to change this date

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.