What''s wrong with this ?

  • declare @t varchar(8)

    set @t='Admit'

    select * from @t

    Server: Msg 137, Level 15, State 2, Line 3

    Must declare the variable @t

  • SELECT @t will give you a result. 

    SELECT * FROM @t is treating @t as a Table Variable...

     

    I wasn't born stupid - I had to study.

  • @t contains table name ('Admit')

  • Is this what you want?

    DECLARE @t TABLE( Field1 varchar(10))

    INSERT INTO @t VALUES( 'Admit')

    SELECT * FROM @t

    I wasn't born stupid - I had to study.

  • You can do:

    use pubs

    go

    declare @t varchar(8)

    set @t='authors'

    exec ('select * from '+@t+'')

    or

    use pubs

    go

    declare @t varchar(8), @cmd nvarchar(100)

    set @t='authors'

    set @cmd = N'select * from '+@t+''

    exec sp_executesql @cmd

    Doing the way that you are trying to do, you cannot select from a variable unless it is a table variable.

  • "you cannot select from a variable unless it is a table variable"

    That's what I wanted to hear, thnx.

Viewing 6 posts - 1 through 5 (of 5 total)

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