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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:09:08+00:00 2026-06-11T14:09:08+00:00

Possible Duplicate: How to use table variable in a dynamic sql statement? If I

  • 0

Possible Duplicate:
How to use table variable in a dynamic sql statement?

If I do what I want to do with a TEMPORARY TABLE, it works fine:

DECLARE @CTRFR VARCHAR(MAX)

SET @CTRFR = 'select blah blah blah' -- <-- very long select statement. this returns a 0 or some greater number. Please note! --> I NEED THIS NUMBER.

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo][#CTRFRResult]') AND type IN (N'U') ) 
   DROP TABLE [dbo].[#CTRFRResult]

CREATE TABLE #CTRFRResult
(
  CTRFRResult VARCHAR(MAX)
)

SET @CTRFR = 'insert into #CTRFRResult ' + @CTRFR
EXEC(@CTRFR)

The above works fine.

The problem is that several databases are using the same TEMP table. Therefore I need to use a VARIABLE table (instead of a temporary table).

What I have below is not working because it says that the table must be declared.

DECLARE @CTRFRResult TABLE
(
   CTRFRResult VARCHAR(MAX)
)

SET @CTRFR = 'insert into @CTRFRResult ' + @CTRFR -- I think the issue is here.
EXEC(@CTRFR)

Setting @CTRFR to insert into... is not working because I’m assuming the table name is out of scope. How would I go about mimicking the temporary table code using a variable table?

The error message I’m getting is:

Must declare the table variable “@CTRFRResult”

  • 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-11T14:09:09+00:00Added an answer on June 11, 2026 at 2:09 pm

    You can’t use an @ table with dynamic SQL like that, you’d have to create the table within the dynamic sql statement as well.

    But I don’t think you really have a problem at all with using # tables.

    The # table will only be available to the current session – you can have 100s of sessions each creating their own version of it with no problems.

    If you want to create a temp table that other sessions can access, you need to use ## instead.

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

Sidebar

Related Questions

Possible Duplicate: What's the difference between a temp table and table variable in SQL
Possible Duplicate: Rename Oracle Table or View I know you can use ALTER TABLE
Possible Duplicate: How to use jQuery to add a new row to a table,
Possible Duplicate: SQL Server: Only last entry in GROUP BY I have a table
Possible Duplicate: What is the use of GO in SQL Server Management Studio? I
Possible Duplicate: Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?
Possible Duplicate: Create a date with T-SQL I've a data table that stores each
Possible Duplicate: Does it make sense to use the <table> tag on a “modern”
Possible Duplicate: Access Auto-Increment Value During INSERT INTO Statement I want to run an
Possible Duplicate: When should use doctrine ORM and when zend-db-table? In short, what is

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.