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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:02:45+00:00 2026-05-24T03:02:45+00:00

Let us say I have a query like this: SELECT * FROM ( SELECT

  • 0

Let us say I have a query like this:

SELECT * FROM 
(
  SELECT * FROM 
  (
    SELECT * FROM DB.dbo.Table
  )
  INNER JOIN DB.dbo.Table ON ...

I am running this query multiple times with different tables by manually changing the string everywhere. I tried declaring the following:

DECLARE @tablename AS VARCHAR(255)
SET @tablename = 'DB.dbo.Table'

But this does not seem to work as it throws me an error saying that I need to declare @tablename as a table variable before I can use it. How do I templatize my table name and if that is possible, will Intellisense still work?

  • 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-24T03:02:46+00:00Added an answer on May 24, 2026 at 3:02 am

    You can wrap it in an EXEC statement like this:

    declare @my_tablename nvarchar(100) = 'mytable';
    exec('
    SELECT * FROM 
    (
      SELECT * FROM 
      (
        SELECT * FROM ' + @my_tablename + '
      )
      INNER JOIN ' + @my_tablename + ' ON ...'
    );
    

    But no, intellisense will not work in that scenario.

    If you know what your output will look like in advance, then you can declare a temp table to hold the results, and then you can access that without EXEC. You will have intellisense on the temp table.

    For example:

      --this must match whatever your SELECT is going to return
      CREATE TABLE #results(
        FIELD1 INT
       ,FIELD2 NVARCHAR(100)
       ,FIELD3 BIT
       );
    
    EXEC('
      INSERT INTO #results(field1,field2,field3)
      SELECT FIELD1,FIELD2,FIELD3 FROM ' + @my_tablename
    );
    
    select * from #results  --you will have intellisense on #results
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a query like this: SELECT * FROM table_name WHERE venue
Let's say we have this query SELECT * FROM table And this result from
I have SQL query like SELECT *, dbo.func(@param1, a.point) as fValue FROM dbo.table AS
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Here I have a query like below: SELECT field FROM table WHERE value IN
Let's say I have a table Employee like this EmpID, EmpName 1 , hatem
Let's say I have a table called Customer, defined like this: Id Name DepartmentId
Let's say, I have a normal normalized database like this: CREATE TABLE `users` (
Let's say I have this data in a MSSQL table type status a open
Let's just say I have an XML file that looks like this: <?xml version=1.0

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.