sql语句判断默认值为getdate()的约束是否存在

时间:2021-12-21 09:53:16

TFlowCreateTask--表名

AddDate--字段名

if not exists(
select d.name
from syscolumns a
join sysobjects b on a.id=b.id
join syscomments c on a.cdefault=c.id
join sysobjects d on c.id=d.id
where b.name='TFlowCreateTask'
and (a.name='AddDate')
)
ALTER TABLE [dbo].[TFlowCreateTask] ADD CONSTRAINT [DF_TFlowCreateTask_AddDate] DEFAULT (getdate()) FOR [AddDate]
GO

 

 

if not exists(
SELECT *
FROM sys.default_constraints dc
WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[DF_TFlowCreateTask_AddDate]')
AND parent_object_id = OBJECT_ID(N'[dbo].[TFlowCreateTask]')
)

ALTER TABLE [dbo].[TFlowCreateTask] ADD CONSTRAINT [DF_TFlowCreateTask_AddDate] DEFAULT (getdate()) FOR [AddDate]
GO