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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:30:37+00:00 2026-06-15T19:30:37+00:00

I have a need for a single select statement that runs another select statement

  • 0

I have a need for a single select statement that runs another select statement within its from clause and the result (there’s only one record) is the table name of the outer select.

For this purpose, I cannot use a stored procedure, exec, or @ variables.

Also, the query file must start with ‘select’ and contain only that statement.

I know there are better ways to accomplish the end result, however the vendor’s wizard that the query must run through sniffs for anything other than a single select statement. Its frustrating, but that’s all we have to work with.

Here is an example of what I am trying to do:

select
  plan.TransactionID,
  plan.PlanName,
  sum(plan.Value) as Rate 
from
  (select 'dbo._Result' + ltrim(str(CalculationID)) from dbo.Calculation where Name = 
    'TPRL Transaction Plan Rates'
  ) Plan

Of course, this does not work. However, neither does this:

select
  plan.TransactionID,
  plan.PlanName,
  sum(plan.Value) as Rate 
from
  exec(select 'dbo._Result' + ltrim(str(CalculationID)) from dbo.Calculation where Name = 'TPRL Transaction Plan Rates') Plan

Even though, this by itself does pull back the table I want into a result set:

exec(select 'dbo._Table' + ltrim(str(CalculationID)) from dbo.Calculation where Name = 'TPRL Transaction Plan Rates')

All I would need for it to do is treat the result as a literal (as in, macro substitution) in the from above, but it doesn’t work that way.

Is there a way in tsql to do this?

  • 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-06-15T19:30:38+00:00Added an answer on June 15, 2026 at 7:30 pm

    No, this cannot be done in just non-dynamic TSQL. In TSQL, table names can not be a variable or parametized, that can only be done with dynamic TSQL.

    And dynamic TSQL can only be done with either EXEC('sql command') or sp_ExecuteSql(..).

    Possible ways around this:
    Unless the facility that you are using has its own macro substitution, then either

    1. Have your client execute the inner SQL query first to determine the table name, then create a new file/string based on that. or
    2. Use some kind of hacker SQL Injection tricks to fool your facility into thinking that you’re executing a SELECT when you are realling execuitng one of the dynamic SQL commands above.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a select statement that gathers data from one monthly-summary table like the
I have two tables described below. What I need is a single query that
I have two different modules that need access to a single file (One will
I have an if statement, and I need to compare a single string with
I have a SQL Select statement where I need to return certain values depending
I have a select statement returning 5 columns: select col1,col2,col3,col4,col5 from table1; col1 col2
I need to have various text view on a single line. And I want
i have a single filed which i need to clone with a clone button
i have a single video (duration: 3 seconds) and i need to create 2
I have got a single file in which I need specify classes in multiple

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.