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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:43:35+00:00 2026-05-29T16:43:35+00:00

I have the following code: DECLARE @temp_table_1 TABLE (id int identity(0, 1), col_1 varchar(50)),

  • 0

I have the following code:

DECLARE @temp_table_1 TABLE (id int identity(0, 1), col_1 varchar(50)), 
        @txtVar VARCHAR(MAX)

INSERT INTO @temp_table_1
  SELECT col_1 FROM table_1  -- This table_1 is a real table in the database.

Set @txtVar = 'SELECT * FROM @temp_table_1'

EXECUTE (@txtVar)

The error I get is

Declare variable @temp_table_1.

How can I fix 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-05-29T16:43:36+00:00Added an answer on May 29, 2026 at 4:43 pm
    Set @txtVar = 'SELECT * FROM myTable WHERE column_value=''' + @var1 + ''''
    

    This article will help you get a basic ideas of dynamic sql.

    EDIT
    It is not possible to use table variables in a dynamic query.
    You have to use temporary table or Use custom TABLE type.

    Temporary table

     CREATE TABLE #temp_table_1 
      ( 
         id    INT IDENTITY(0, 1), 
         col_1 VARCHAR(50) 
      ) 
    
    DECLARE @txtVar VARCHAR(MAX) 
    
    INSERT INTO #temp_table_1 
    
    
    SELECT col_1 
    FROM   table_1 -- This table_1 is a real table in the database. 
    SET @txtVar = 'SELECT * FROM  #temp_table_1' 
    
    EXECUTE (@txtVar) 
    
    DROP TABLE #temp_table_1 
    

    Custom Table Type

    CREATE TYPE DefaultTable AS TABLE (ID INT IDENTITY(0, 1), COL_1 VARCHAR(50))
    GO
    
    -- Fill a var of that type with some test data
    DECLARE @MyTable DefaultTable
    INSERT @MyTable 
    SELECT col_1 FROM table_1 -- This table_1 is a real table in the database.
    
    -- Now this is how you pass that var into dynamic statement
    EXECUTE sp_executesql N'SELECT * FROM @MyTable', 
        N'@MyTable DefaultTable READONLY', 
        @MyTable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: DECLARE @monthPassed VARCHAR(MAX) SET @monthPassed = '2010' DECLARE @yearPassed
I have the following code in a stored procedure. .... select ... into #temp
I have the following code: #include<stdio.h> int main() { int(* a)[10]; //declare a as
Currently in a simple form i have the following declared table in code: declare
I have the following code: #include <iostream> using namespace std; class testing{ int test()
Suppose you have the following code: int main(int argc, char** argv) { Foo f;
I have the following code switch (xn.SelectSingleNode(@type).InnerText) { case int: case int16: case int32:v=int.Parse(xn.InnerText);break;
I have the following code which works fine and returns the expected results: DECLARE
I have the following code: public void DeleteAccountsForMonth(int year, int month) { var result
I have the following code where I declare a PHP array variable and inside

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.