Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop Column

Status
Not open for further replies.

Ladyhawk

Programmer
Joined
Jan 22, 2002
Messages
534
Location
AU
I can't drop a particular column in a table because it has a default value. So how can I drop the default value via SQL?

Ladyhawk. [idea]
** ASP/VB/Java Programmer **
 
You can drop a default value (contraint) using

ALTER TABLE MyTable DROP CONSTRAINT {constraint name}
 
But I don't know the constraint associated with a default value.

Ladyhawk. [idea]
** ASP/VB/Java Programmer **
 
This is based on an article in BOL "Working with Default Constraints" worth a read

DECLARE @defname VARCHAR(100), @cmd VARCHAR(1000)
SET @defname =
(SELECT name
FROM sysobjects so JOIN sysconstraints sc
ON so.id = sc.constid
WHERE object_name(so.parent_obj) = 'MyTable'
AND so.xtype = 'D'
AND sc.colid =
(SELECT colid FROM syscolumns
WHERE id = object_id('dbo.MyTable') AND
name = 'MyColumn'))
SET @cmd = 'ALTER TABLE MyTable DROP CONSTRAINT '
+ @defname
EXEC(@cmd)

ALTER TABLE MyTable DROP Column MyColumn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top