Groups | Blog | Home
all groups > sql server programming > october 2006 >

sql server programming : Check for constraints on a field


Maurice
10/26/2006 11:59:21 PM
Hi,

I want to remove a field from a table by using 'ALTER TABLE tblTest DROP
[TST_Available]'. I got an error message returned that there is a
constraint on the field (the field has a default value).

How do I programmaticaly check for constraints on a specific field and
delete them?
Maurice
10/27/2006 12:32:01 AM
Hi Nick,

I tried this but the only constraints this SQL shows are the primary keys
in the tables, not the 'default constraints'.

I can see the constraints by using MS SQL Server Management Studio.
Maurice
10/27/2006 1:05:06 AM
Hi Tibor,

but how come when entering a default value for a column in a table an
object in the constraints is generated specifying this default value.

I just tried the sys.columns, it gives me indeed information about the
column. The column default_oject_id is filled with an object_id. Do you
Maurice
10/27/2006 1:11:28 AM
Thx,

found the solution.

using sp_helpconstraint 'TABLENAME' gives me the name of the constraint for
the default value. After this I just delete the constraint and the drop the
Maurice
10/27/2006 1:32:01 AM
Hi Prad,

thx! I will try it...

Also I will follow the advice of Tibor and name the constraints.
ML
10/27/2006 2:26:02 AM
Nick
10/27/2006 8:04:55 AM
Have a look at the view:
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE

[quoted text, click to view]

Tibor Karaszi
10/27/2006 9:41:57 AM
A default in ANSI SQL is a column property (like NOT NULL in SQL Server), not a constraint. Because
of that, default constraints are not exposed in the information schema views.

How about using sys.columns instead? It has a column called default_object_id.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


[quoted text, click to view]
Tibor Karaszi
10/27/2006 10:16:11 AM
[quoted text, click to view]

That is because this type of default is implemented as constraints in SQL Server. This is just how
the product work. You should always name your constraint (including default constraints) so you then
know the name in advance.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


[quoted text, click to view]
Prad
10/27/2006 1:49:37 PM
try this:

IF EXISTS ( SELECT 'x'
FROM syscolumns
JOIN dbo.sysobjects obj1
ON syscolumns.id = obj1.id
JOIN dbo.sysobjects obj2
ON obj2.id = syscolumns.cdefault
WHERE obj1.id = OBJECT_ID(N'[table_name]') and SYSCOLUMNS.name
='Field_Name')
BEGIN
WHILE EXISTS ( SELECT 'x'
FROM syscolumns
JOIN dbo.sysobjects obj1
ON syscolumns.id = obj1.id
JOIN dbo.sysobjects obj2
ON obj2.id = syscolumns.cdefault
WHERE obj1.id = OBJECT_ID(N'[table_name]]') and
SYSCOLUMNS.name ='Field_Name')
BEGIN
DECLARE @ConstraintName sysname
SELECT TOP 1 @ConstraintName = obj2.name
FROM syscolumns
JOIN dbo.sysobjects obj1
ON syscolumns.id = obj1.id
JOIN dbo.sysobjects obj2
ON obj2.id = syscolumns.cdefault
WHERE obj1.id = OBJECT_ID(N'[table_name]]') and
SYSCOLUMNS.name ='Field_Name'
exec('ALTER TABLE [dbo].[table_name]] DROP CONSTRAINT ['+
@ConstraintName + ']')

END
END
[quoted text, click to view]

AddThis Social Bookmark Button