By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,045 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,045 IT Pros & Developers. It's quick & easy.

Stored Procedure to change default

P: n/a
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

Dec 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Maybe 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: mi**@epprecht.net

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/

<JL*******@Enlighten.Net> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.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

Dec 14 '05 #2

P: n/a
Thanks, but that didn't do it. I get the same error:

Line 3: Incorrect syntax near '@oname'

Dec 14 '05 #3

P: n/a
(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
Dec 14 '05 #4

P: n/a
>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

Dec 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.