Using Variables in the FROM clause

Results 1 to 4 of 4

Thread: Using Variables in the FROM clause

  1. #1
    fifthermurf Guest

    Default Using Variables in the FROM clause

    I am a beginning T-SQL programmer is there anyway to use a variable such as a parameter to signify a table name and then use that in the FROM clause of a simple query or<BR>[like :]<BR>@table_name varchar(30) = NULL<BR><BR>SELECT * FROM @table_name<BR><BR>I do this and the SQL statement errors out. There must be a way? Do I use another table ? System table ?<BR><BR>HELP! Thanks<BR><BR>

  2. #2
    Garth Guest

    Default RE: Using Variables in the FROM clause

    You need to use the EXEC() statement...<BR><BR>USE Northwind<BR>go<BR>DECLARE @table_name varchar(30)<BR>SET @table_name = &#039Customers&#039<BR>EXEC(&#039SELECT * FROM &#039 + @table_name)<BR><BR>--<BR>Garth<BR>

  3. #3
    Join Date
    Dec 1969

    Default Use Dynamic EXEC statement

    See:<BR>Using Dynamic SQL Statements in Stored Procedures <BR><BR><BR>The Speed of Dynamic Queries<BR><BR><BR>

  4. #4
    fifthermurf Guest

    Default GREAT THANKYOU ,


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts