Wednesday, March 21, 2012

DROP TABLE?

hi, why I can do this:
DECLARE @.table_name varchar(100)
SET @.table_name ='ABC'
DROP TABLE @.table_name
Line 3: Incorrect syntax near '@.table_name'http://www.sommarskog.se/dynamic_sql.html
This is my signature. It is a general reminder.
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
"js" <js@.someone@.hotmail.com> wrote in message
news:uDYbxIQQFHA.3664@.TK2MSFTNGP15.phx.gbl...
> hi, why I can do this:
> DECLARE @.table_name varchar(100)
> SET @.table_name ='ABC'
> DROP TABLE @.table_name
> Line 3: Incorrect syntax near '@.table_name'
>|||js wrote:
> hi, why I can do this:
> DECLARE @.table_name varchar(100)
> SET @.table_name ='ABC'
> DROP TABLE @.table_name
> Line 3: Incorrect syntax near '@.table_name'
Because the function requires an object literal, not a variable.
You can't even say:
Drop Table 'ABC'
You need to say:
Drop Table ABC
or
Drop Table dbo.ABC
David Gugick
Imceda Software
www.imceda.com|||js wrote:
> hi, why I can do this:
> DECLARE @.table_name varchar(100)
> SET @.table_name ='ABC'
> DROP TABLE @.table_name
> Line 3: Incorrect syntax near '@.table_name'
I should have added that if you want to perform a dynamic DROP, you can
use dynamic SQL:
Declare @.SQL nvarchar(128)
Set @.SQL = N'Drop Table dbo.ABC'
Exec sp_executesql @.SQL
David Gugick
Imceda Software
www.imceda.com|||DROP TABLE doesn't accept a variable. What you can do is:
DECLARE @.table_name SYSNAME
SET @.table_name ='Employees'
EXEC ('DROP TABLE' + @.table_name)
Jacco Schalkwijk
SQL Server MVP
"js" <js@.someone@.hotmail.com> wrote in message
news:uDYbxIQQFHA.3664@.TK2MSFTNGP15.phx.gbl...
> hi, why I can do this:
> DECLARE @.table_name varchar(100)
> SET @.table_name ='ABC'
> DROP TABLE @.table_name
> Line 3: Incorrect syntax near '@.table_name'
>|||...yielding one of the weaknesses here, difficult parsing...
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'TABLEEmployees'.
:-)
This is my signature. It is a general reminder.
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
"Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid> wrote
in message news:er1E1NQQFHA.3416@.TK2MSFTNGP10.phx.gbl...
> DROP TABLE doesn't accept a variable. What you can do is:
> DECLARE @.table_name SYSNAME
> SET @.table_name ='Employees'
> EXEC ('DROP TABLE' + @.table_name)
> --
> Jacco Schalkwijk
> SQL Server MVP
>
> "js" <js@.someone@.hotmail.com> wrote in message
> news:uDYbxIQQFHA.3664@.TK2MSFTNGP15.phx.gbl...
>|||Note to self: should use fixed-width fonts :)
Jacco Schalkwijk
SQL Server MVP
"Aaron [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:u8OwKVQQFHA.3448@.TK2MSFTNGP10.phx.gbl...
> ...yielding one of the weaknesses here, difficult parsing...
> Server: Msg 170, Level 15, State 1, Line 1
> Line 1: Incorrect syntax near 'TABLEEmployees'.
> :-)
> --
> This is my signature. It is a general reminder.
> Please post DDL, sample data and desired results.
> See http://www.aspfaq.com/5006 for info.
> "Jacco Schalkwijk" <jacco.please.reply@.to.newsgroups.mvps.org.invalid>
> wrote in message news:er1E1NQQFHA.3416@.TK2MSFTNGP10.phx.gbl...
>|||js wrote:
> hi, why I can do this:
> DECLARE @.table_name varchar(100)
> SET @.table_name ='ABC'
> DROP TABLE @.table_name
> Line 3: Incorrect syntax near '@.table_name'
--BEGIN PGP SIGNED MESSAGE--
Hash: SHA1
You cannot use a variable for the table name in the command "Drop
Table."
You can, however, use dynamic SQL, which, in this case is VERY
DANGEROUS!
Use at your own risk:
DECLARE @.table_name varchar(100)
SET @.table_name ='ABC'
EXEC ('DROP TABLE ' + @.table_name)
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
--BEGIN PGP SIGNATURE--
Version: PGP for Personal Privacy 5.0
Charset: noconv
iQA/ AwUBQmKZloechKqOuFEgEQJfjgCfX++bziZp74Kw
80hlHNV+y+UZ9akAoLO4
zHlTEuZb1E7oAuG7vBq0IjwD
=zRUS
--END PGP SIGNATURE--

No comments:

Post a Comment