문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top