I would like to check the column already has default constraint or not only with the table name and column name data.With that record count we can create the constraint.
Do we have any other property to check the constraint exists for that column in dataset?
the query
select COLUMN_DEFAULT
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = @tableName
and COLUMN_NAME = @columnName
will return the default value specified for a column (null
if no default was defined)
Update:
The following query will also retrieve the constraint's name:
select
dc.name,
dc.definition
from sys.default_constraints dc
join sys.objects o
on o.object_id = dc.parent_object_id
join sys.columns c
on o.object_id = c.object_id
and c.column_id = dc.parent_column_id
where o.name = @tableName
and c.name = @columnName
If no rows are returned, then there's no default constraint defined on the column.