Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Stored Procedure to change default
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


Report this thread to moderator Post Follow-up to this message
Old Post
JLavalley@Enlighten.Net
12-15-05 01:25 AM


Re: Stored Procedure to change default
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: 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
>



Report this thread to moderator Post Follow-up to this message
Old Post
Mike Epprecht \(SQL MVP\)
12-15-05 01:25 AM


Re: Stored Procedure to change default
Thanks, but that didn't do it.  I get the same error:

Line 3: Incorrect syntax near '@oname'


Report this thread to moderator Post Follow-up to this message
Old Post
JLavalley@Enlighten.Net
12-15-05 01:25 AM


Re: Stored Procedure to change default
(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

Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
12-15-05 01:25 AM


Re: Stored Procedure to change default
>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


Report this thread to moderator Post Follow-up to this message
Old Post
JLavalley@Enlighten.Net
12-15-05 04:23 PM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 11:21 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006