我在SO上找到了以下代码,稍微修改了变量名称。
-- If data type differs, drop constraints and change data type.
WHILE 0=0
BEGIN
SET @ConstraintName = (SELECT TOP 1 constraint_name FROM information_schema.constraint_column_usage WHERE table_name = @TableName and column_name = @FieldName)
IF @ConstraintName IS NULL BREAK;
EXEC('ALTER TABLE ' + @TableName + ' DROP CONSTRAINT "' + @ConstraintName + '"');
END
EXEC('ALTER TABLE ' + @TableName + ' ALTER COLUMN ' + @FieldName + ' ' + @FieldType + ' NOT NULL');
虽然放弃了在信息模式中找到的所有约束,但后续执行仍然会抛出错误:
The object 'DF_settings_monitoring' is dependent on column 'monitoring'.
ALTER TABLE ALTER COLUMN monitoring failed because one or more objects access this column.
在某些情况下。只有一些,而不是全部。
我发现information_schema.constraint_column_usage
表不包含所有默认约束,但我不确定原因。
是否有关于特定列的现有约束的更可靠的信息来源?
答案 0 :(得分:1)
在回答here时,information_schema不包含默认约束。
所以如果你用这个替换你的选择:
select top 1 name from sys.default_constraints
... 你应该好好去。