Select a table from a variable ?

Last post 05-30-2008, 2:49 AM by Phil Factor. 1 replies.
Sort Posts: Previous Next
  •  05-29-2008, 9:49 PM Post number 56892

    Select a table from a variable ?

    Hi all .
    I have a problem that i can not solve for so long already . Do anyone know how to Select a table from a variable  ( not use table variable ) .
    Example :

    Declare @fName varchar(16)
    Set @fName =  'customer'
    Select * from @fName

    This one does not work,  of course . Do anyone know how to solve it  ( not Exec ('select * from' + @fName) ) . Or anyone can explain for me that why the code above return this message  : Must declare the variable @fname . ?
    Regards .
  •  05-30-2008, 2:49 AM Post number 56992 in reply to post number 56892

    Re: Select a table from a variable ?

    You've declared a nice scalar variable called @FName. You've then used a table variable called @FName. This confused SQL Server, because it couldn't find a table variable called @Fname. I realise that the error  message is misleading. It should be '@fname used as a table variable when defined as a scalar.
    Try:
    DECLARE @fName VARCHAR(16)
    SET @fName = 'customer'
    SELECT [customer]= @fName










View as RSS news feed in XML