Drop Table
Support Forum for database administrators and web based access to important newsgroups related to databasesCan 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
Post Follow-up to this messageMaybe more than one row is returned, and only one value can fit into a variable. This will work: declare @oname sysname select TOP 1 @oname=name from sysobjects where name like "df__mytable__mycol%" alter table mytable drop constraint @oname Regards -------------------------------- Mike Epprecht, Microsoft SQL Server MVP Zurich, Switzerland IM: mike@epprecht.net MVP Program: http://www.microsoft.com/mvp Blog: http://www.msmvps.com/epprecht/ <JLavalley@Enlighten.Net> wrote in message news:1134587099.580770.161300@g44g2000cwa.googlegroups.com... > 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 >
Post Follow-up to this messageThanks, but that didn't do it. I get the same error: Line 3: Incorrect syntax near '@oname'
Post Follow-up to this message(JLavalley@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, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx
Post Follow-up to this message>Why should it work?< What kind of question is that? Not relevant here. I asked why it doesn't... thank you for your response, not only did you solve my problem (using "exec") but you educated me on the fact that I can't use variables for object names in most places. PS. It should work because it's much more intuitive than having to create and execute a string of the same command. It should work because that would be consistant with other T-SQL commands
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread