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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:19:58+00:00 2026-06-03T10:19:58+00:00

Is this incorrect, can’t we pass the table name to a select query dynamically?

  • 0

Is this incorrect, can’t we pass the table name to a select query dynamically?

This is giving me a error ‘Must declare the table variable @TblName’

DECLARE @TblName VARCHAR(30)
SET @TblName = 'User'
SELECT * 
FROM @TblName
  • 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-03T10:20:00+00:00Added an answer on June 3, 2026 at 10:20 am

    You need to create a dynamic SQL query, preferably using the QUOTENAME function. You can avoid any issues from malicious input by using QUOTENAME function.

    Here is a sample script that illustrates how to query a table by creating a dynamic SQL query by passing in a table name. You can change the table name by value to the variable @tablename.

    Create and insert script for sample:

    CREATE TABLE sample
    (
        id INT NOT NULL
    );
    
    INSERT INTO sample (id) VALUES
      (1),
      (2),
      (3),
      (4),
      (5),
      (6);
    

    Dynamic SQL script:

    DECLARE @execquery AS NVARCHAR(MAX)
    DECLARE @tablename AS NVARCHAR(128)
    
    SET @tablename = 'sample'
    SET @execquery = N'SELECT * FROM ' + QUOTENAME(@tablename)
    
    EXECUTE sp_executesql @execquery
    

    Demo:

    Click here to view the demo in SQL Fiddle.

    Suggested read:

    The Curse and Blessings of Dynamic SQL

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

Sidebar

Related Questions

I am getting an error Incorrect syntax near ',' for this query on line
I get this error when I run the application Incorrect syntax near 12 ,
I have a table that looks like this: id datatype name value 0 nvarchar(255)
I am getting this weird error message can not sort on multivalued field: fieldname
I asked this function based on this concept (maybe incorrect?!): Wherever a const can
Having this method: public Boolean isCorrect() { return correct; } I can't write: @XmlType(propOrder
Possible Duplicate: Why does this intentionally incorrect use of strcpy not fail horribly? Below
This is my statement Why I get those errors Incorrect syntax near the keyword
When I try to run this code, I get incorrect maximums and minimums. Could
This somehow simple task is not so simple. I can get the number of

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.