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

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use .live() instead of .click(): http://api.jquery.com/live/ $('.clickme').live('click', function() { //… May 16, 2026 at 3:39 pm
  • Editorial Team
    Editorial Team added an answer Has anyone had any experience with a solution similar to… May 16, 2026 at 3:38 pm
  • Editorial Team
    Editorial Team added an answer I'd write the bare minimum to keep the linter quiet.… May 16, 2026 at 3:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

For example I have the following tables resulting from: CREATE TABLE A (Id int,
I have a table. The table needs to store a number values about a
I have an Oracle Date type to which I need to insert the current
I have an SQL Server DB with a table with these fields: A bit
I have a user defined function in a SQL Server 2005 database which returns
I'm trying to modify the following code so that it will return a Dictionary<int,int>
I have an existing ASP.NET app which is configured to connect to a MS
I've got a couple of Linq to SQL entities which are causing me problems:
Given the following HTML: <asp:content id=Content1 contentplaceholderid=mainContent runat=server> <div class=scrollRow> <table width=100%> <tr> <td
I have a database running on MS SQL Server 2005 and an ASP.NET 3.5

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.