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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:15:45+00:00 2026-05-30T17:15:45+00:00

Is there a way to code a dynamic inner query? Basically, I find myself

  • 0

Is there a way to code a dynamic inner query? Basically, I find myself typing something like the following query over and over:

;with tempData as (
   --this inner query is the part that changes, but there's always a timeGMT column.
   select timeGMT, dataCol2, dataCol3
   from tbl1 t1
      join tbl2 t2 on t1.ID=t2.ID
)
select dateadd(ss,d.gmtOffset,t.timeGMT) timeLocal,
   t.*
from tempData t
   join dst d on t.timeGMT between d.sTimeGMT and d.eTimeGMT
where d.zone = 'US-Eastern'

The only thing I can think of is a stored proc with the inner query text as the input for some dynamic sql… However, my understanding of the optimizer (which is, admittedly, limited) says this isn’t really a good idea.

  • 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-30T17:15:46+00:00Added an answer on May 30, 2026 at 5:15 pm

    From a performance perspective, what you have there is the version on which I would expect the optimizer to do the best job.

    If the “outer” part of your example is static and code maintenance overrides performance, I’d look to encapsulating the dateadd result in a table-valued function (TVF). Since the time conversion is very much the common thread in these queries, I would definitely focus on that part of the workload.

    For example, your query that can vary would look like this:

    select timeGMT, dataCol2, dataCol3, lt.timeLocal
    from   tbl1 t1
           join tbl2 t2 on t1.ID = t2.ID
           cross apply dbo.LocalTimeGet(timeGMT, 'US-Eastern') AS lt
    

    Where the TVF dbo.LocalTimeGet contains the logic for dateadd(ss,d.gmtOffset,t.timeGMT) and the lookup of the time zone offset value based on the time zone name. The implementation of that function would look something like:

    CREATE FUNCTION dbo.LocalTimeGet (
        @TimeGMT  datetime,
        @TimeZone varchar(20)
    )
    RETURNS TABLE
    AS
    RETURN (
        SELECT  DATEADD(ss, d.gmtOffset, @TimeGMT) AS timeLocal
        FROM    dst AS d
        WHERE   d.zone = @TimeZone
    );
    GO
    

    The upside of this approach is when you upgrade to 2008 or later, there are system functions you could use to make this conversion a lot easier to code and you’ll only have to alter the TVF. If your result sets are small, I’d consider a system scalar function (SQL 2008) over a TVF, even if it implements those same system functions. Based on your comment, it sounds like the system functions won’t do what you need, but you could still stick with your implementation of a dst table, which is encapsulated in the TVF above.

    TVFs can be a performance problem because the optimizer assumes they only return 1 row.

    If you need to combine encapsulation and performance, then I’d do the time zone calc in the application code instead. Even though you’d have to apply it to each project that uses it, you would only have to implement it 1x in each project (in the Data Access Layer) and treat it as a common utility library if you’ll be using across projects.

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

Sidebar

Related Questions

Is there a way to execute code after each Terminal? So that something like
Is there a way to tidy-up the following code, rather than a series of
Is there a way to enable code outlining for Classic ASP in Visual Studio
Is there way in next piece of code to only get the first record?
Is there a way to reformat code, i.e. force correct indentation in FlashDevelop as
Is there a way to test code coverage within visual studio if I'm using
Is there any way to improve code completion in notepad++? Currently it supports some
Is there a way to complie code directly into Native Code instead of MSIL
Is there a way to automate code signing a VBA project in a Word
Is there a way I can control columns from code. I had a drop

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.