(JL*******@Enlighten.Net) writes:
Can someone explain to me why the following doesn't work?
declare @oname sysname
select @oname=name from sysobjects where name like
"df__mytable__mycol%"
alter table mytable drop constraint @oname
Why should it work? There are some places you can use variables in
T-SQL for object names, but in most places you can't. Check out the
syntax chart in Books Online when in doubt.
You can use dynamic SQL: EXEC('ALTER TABLE tbl DROP CONSTRAINT ' + @oname)
For more information about dynamic SQL, see this article on my web site:
http://www.sommarskog.se/dynamic_sql.html.
--
Erland Sommarskog, SQL Server MVP,
es****@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx