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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Default Constraint

Status
Not open for further replies.

agoeddeke

Programmer
Jul 23, 2002
201
US
Hello,

How can I check to see if a default constraint exists on a column and what the name of the constraint is? I want to eventually drop the column, but need to check and drop the constraint first.

Thanks,
Andy
 
Is this a default you created explicitly or within the table definition?


If explicit, you should be able to query sysobjects for the name you gave it. If in the table def, I am sure you can still do the same thing, but figuring out the name SQL Server gave the constraint might be a little involved...

HTH,
John
 
john76 said:
Is this a default you created explicitly or within the table definition?
The name was created within the table definition.

john76 said:
...but figuring out the name SQL Server gave the constraint might be a little involved...
This is exactly what I'm trying to figure out.

Thanks,
Andy

 
Just to follow up... this is what I ended up with for anyone interested:

Code:
DECLARE @cTable VARCHAR(100), @cColumn VARCHAR(100)
SET @cTable = 'myTable'
SET @cColumn = 'myColumn'
SELECT sysobjects.name from sysobjects
	JOIN syscolumns ON 
		(sysobjects.id = syscolumns.cdefault 
		AND sysobjects.parent_obj = syscolumns.id)
	WHERE sysobjects.parent_obj = object_id(@cTable) 
		AND sysobjects.xtype IN 
		 ('C ','PK','UQ','F ', 'D ')
		AND syscolumns.name = @cColumn

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top