Using variable for table name

Posted on 2008-11-19
Last Modified: 2012-05-05
hello experts,

how can i use a variable for the table name?

declare @table_name varchar(2000)

set @table_name = 'mytable'

select *
  from @table_name

I would have thought it was that easy, but it wants me to declare the table variable.  i read up on table variables, and that talks about storing values...not wanting to do that.

Question by:thewayne73
    LVL 44

    Accepted Solution

    Hello thewayne73,

    declare @table_name varchar(2000)

    set @table_name = 'mytable'

    Exec ('select *
      from ' + @table_name)


    LVL 44

    Expert Comment


    You have to use Dynamic sql, in otherwords build your required statement as a string and then use exec() to actually execute the statement. Sql Server does not do direct macro replacement in the way you were thinking.

    LVL 11

    Expert Comment


    DECLARE @Query NVARCHAR(2000)
    DECLARE @table_name VARCHAR(50)
    SET @table_name = 'mytable'
    SET @Query = 'SELECT  * FROM ' + @table_name
    EXECUTE sp_executesql @Query

    Open in new window


    Author Closing Comment

    Thanks...i was creating dynamic sql..but that can end up looking nasty.  using this solution will work for most of what i am dong.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
    Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now