public IEnumerable <string> AlterColumn(string tableName, Column column) { // drop (possibly) existing default constraint Table table = GetTable(tableName); var c = new Microsoft.SqlServer.Management.Smo.Column(table, column.Name) { Nullable = column.IsNullable, DataType = Convert(column.DataType), }; DefaultConstraint defaultConstraint = AddDefaultConstraint(c, tableName, "dummy"); defaultConstraint.Drop(); foreach (string command in ScriptChanges(table.Parent.Parent)) { yield return(string.Format(CultureInfo.InvariantCulture, "IF OBJECT_ID('{0}') IS NOT NULL ", defaultConstraint.Name) + command); } // alter column table = GetTable(tableName); c = new Microsoft.SqlServer.Management.Smo.Column(table, column.Name) { Nullable = column.IsNullable, DataType = Convert(column.DataType), }; if (column.DefaultValue != null) { AddDefaultConstraint(c, tableName, column.DefaultValue); } c.Alter(); foreach (string command in ScriptChanges(table.Parent.Parent)) { yield return(command); } }
public SQLScriptList RebuildConstraint(Boolean Check) { SQLScriptList list = new SQLScriptList(); if (DefaultConstraint != null) { if ((!Check) || (DefaultConstraint.CanCreate)) list.Add(DefaultConstraint.Create()); list.Add(DefaultConstraint.Drop()); } return list; }
public IEnumerable <string> DropDefault(string tableName, Column column) { Table table = GetTable(tableName); var c = new Microsoft.SqlServer.Management.Smo.Column(table, column.Name) { DataType = Convert(column.DataType), Nullable = column.IsNullable, }; DefaultConstraint defaultConstraint = AddDefaultConstraint(c, tableName, column.DefaultValue); defaultConstraint.Drop(); return(ScriptChanges(table.Parent.Parent)); }