select from a table

  • How I can use a variable to be a table name in select from sentence. for example:

    select * from @table_name. This way doesn't work. Can some one help me. Thank you.

  • Have a look at Page 2 of this forum - there is a relevant thread ("Select * from @var" possible?") which will probably answer your questions.

    Paul Ibison

    Paul.Ibison@btinternet.com


    Paul Ibison
    Paul.Ibison@replicationanswers.com

  • Follow Pau;s advice. This is not possible directly except with dynamic sql.

    Steve Jones

    steve@dkranch.net

  • Hi, I had the same "problem" and I did this:

    CREATE PROCEDURE spExample

    @namevarchar

    AS

    DECLARE @strSqlvarchar

    DECLARE @tablevarchar

    Set @table = 'TableName' + @name

    Select @strSql = "select * from ' + @table

    Exec (@strSql)

    I hope I help 😀

    Have a great weekend

    Ana


    Ana

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply