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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:19:12+00:00 2026-05-15T07:19:12+00:00

I have the following t-sql code which generates an error Declare @table TABLE (

  • 0

I have the following t-sql code which generates an error

Declare @table TABLE
(
    ID1 int,
    ID2 int
)

INSERT INTO @table values(1, 1); INSERT INTO @table values(2, 2); INSERT INTO @table values(3, 3);

DECLARE @field varchar(50); SET @field = 'ID1'

DECLARE @query varchar(MAX); SET @query = 'SELECT * FROM @table WHERE ' + @field + ' = 1' EXEC (@query)


The error is Must declare the table variable "@table".
What's wrong with the query. How to fix it?

  • 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-15T07:19:13+00:00Added an answer on May 15, 2026 at 7:19 am

    You cannot use the EXEC statement or the sp_executesql stored procedure to run a dynamic SQL Server query that refers a table variable, if the table variable was created outside the EXEC statement or the sp_executesql stored procedure. Because table variables can be referenced in their local scope only, an EXEC statement and a sp_executesql stored procedure would be outside the scope of the table variable. However, you can create the table variable and perform all processing inside the EXEC statement or the sp_executesql stored procedure because then the table variables local scope is in the EXEC statement or the sp_executesql stored procedure.

    Try this may work for you:

    DECLARE @SQLString nvarchar(500);
    DECLARE @ParmDefinition nvarchar(500);
    
    create TABLE #table 
    (
        ID1 varchar(30),
        ID2 int
    )
    
    INSERT INTO #table values(1, 1);
    INSERT INTO #table values(1, 2);
    INSERT INTO #table values(1, 3);
    
    
    
    DECLARE @field varchar(30);
    SET @field = 'ID1'
    
    SET @SQLString = N'SELECT * FROM #table WHERE  @fld  = 1';
    SET @ParmDefinition = N'@fld varchar(30)';
    
    DECLARE @query varchar(MAX);
    SET @query = 'SELECT * FROM #table WHERE ' + @field + ' = 1'
    EXEC (@query)
    
    drop table #table
    
    • 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: def add_record(self,values): self.sql.execute(INSERT INTO TEST VALUES (?,?) % values)
I have following tsql code in sql server 2008: declare @ID INT SET @ID
I have the following shortened classic ASP code that makes a SQL insert call...
I have the following SQL code that runs against a Change Request database. Each
I have the following java code: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
I have the following code that fills dataTable1 and dataTable2 with two simple SQL
Following Uncle Bob's avice in Clean Code , I'd like to have no SQL
For example I have the following tables resulting from: CREATE TABLE A (Id int,
I have been profiling the SQL query NHibernate generates for the following snippets below.
I have a lot of SQL insert/update/delete statements of which some are redundant. For

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.