Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 alter table

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-02-12 : 10:58:42
Hi,
What is wrong with this please?

alter table @tbltest
add lastname varchar(200) null

Error is:
incorrect syntax near @tbltest

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-12 : 11:02:03
you cant use variable like this. for that you need dynamic sql

EXEC('alter table '+ @tbltest + '
add lastname varchar(200) null')

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-02-12 : 11:06:47
quote:
Originally posted by visakh16

you cant use variable like this. for that you need dynamic sql

EXEC('alter table '+ @tbltest + '
add lastname varchar(200) null')

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





It throws the error

Msg 137, Level 15, State 2, Line 2
Must declare the scalar variable "@tbltest".


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-12 : 11:09:08
quote:
Originally posted by senthil_nagore

quote:
Originally posted by visakh16

you cant use variable like this. for that you need dynamic sql

EXEC('alter table '+ @tbltest + '
add lastname varchar(200) null')

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





It throws the error

Msg 137, Level 15, State 2, Line 2
Must declare the scalar variable "@tbltest".


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/



you need to declare it before using.
I assumed OP have done that
or else OP might have got same error

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-02-12 : 11:59:15
thank you
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-12 : 12:07:44
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -