怎样用SQL语句删除一个带有默认值的字段
---------------------------------------------
对Table1用SQL操作,添加一个不带默认值的字段后再用以下方法删除,可以,
但如果添加一个带有默认值的字段,再用以下方法删除,则提示引用出错,不能删除!
Alter Table Table1 Add Field8 Integer DEFAULT 123
Alter Table Table1 Drop Column Field8
12 个解决方案
#1
declare @name varchar(20)
select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('表名') and b.id=a.cdefault and a.name='你的列名' and b.name like 'DF%'
exec('alter table tablename drop constraint '+@name)
select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('表名') and b.id=a.cdefault and a.name='你的列名' and b.name like 'DF%'
exec('alter table tablename drop constraint '+@name)
#2
然后再:
Alter Table Table1 Drop Column Field8
Alter Table Table1 Drop Column Field8
#3
alter table tableName drop constraint '默认值约束名'
alter table talbeName drop column '字段名'
alter table talbeName drop column '字段名'
#4
怎样可以找出 “constraint '默认值约束名'” ?
#5
經測試﹐pengdali(大力 V3.0) 的方法會出現以下錯誤﹕
'DF__Table1__Field8__' is not a constraint.
改為如下即可﹕
declare @name varchar(8000)
select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('Table1') and b.id=a.cdefault and a.name='Field8' and b.name like 'DF%'
exec('alter table Table1 drop constraint '+@name)
Alter Table Table1 Drop Column Field8
'DF__Table1__Field8__' is not a constraint.
改為如下即可﹕
declare @name varchar(8000)
select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('Table1') and b.id=a.cdefault and a.name='Field8' and b.name like 'DF%'
exec('alter table Table1 drop constraint '+@name)
Alter Table Table1 Drop Column Field8
#6
declare @name varchar(8000)
select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('Table1') and b.id=a.cdefault and a.name='Field8' and b.name like 'DF%'
就可以找出 “constraint '默认值约束名'
select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('Table1') and b.id=a.cdefault and a.name='Field8' and b.name like 'DF%'
就可以找出 “constraint '默认值约束名'
#7
怎样可以找出 “constraint '默认值约束名'” ?
呵呵。楼主不仔细看贴。
呵呵。楼主不仔细看贴。
#8
thanks !
有没有通用的SQL语句(支持多个数据库),我现在是在用Java,大家知不知 Java 怎样可以得到 “默认值约束名”?
有没有通用的SQL语句(支持多个数据库),我现在是在用Java,大家知不知 Java 怎样可以得到 “默认值约束名”?
#9
收了
#10
用sql 语句你就别指望跨平台了.
如果用ADOX倒是可以隐藏某些实现细节,只是我不知道java能不能调用adox
如果用ADOX倒是可以隐藏某些实现细节,只是我不知道java能不能调用adox
#11
:)
#12
thanks !
#1
declare @name varchar(20)
select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('表名') and b.id=a.cdefault and a.name='你的列名' and b.name like 'DF%'
exec('alter table tablename drop constraint '+@name)
select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('表名') and b.id=a.cdefault and a.name='你的列名' and b.name like 'DF%'
exec('alter table tablename drop constraint '+@name)
#2
然后再:
Alter Table Table1 Drop Column Field8
Alter Table Table1 Drop Column Field8
#3
alter table tableName drop constraint '默认值约束名'
alter table talbeName drop column '字段名'
alter table talbeName drop column '字段名'
#4
怎样可以找出 “constraint '默认值约束名'” ?
#5
經測試﹐pengdali(大力 V3.0) 的方法會出現以下錯誤﹕
'DF__Table1__Field8__' is not a constraint.
改為如下即可﹕
declare @name varchar(8000)
select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('Table1') and b.id=a.cdefault and a.name='Field8' and b.name like 'DF%'
exec('alter table Table1 drop constraint '+@name)
Alter Table Table1 Drop Column Field8
'DF__Table1__Field8__' is not a constraint.
改為如下即可﹕
declare @name varchar(8000)
select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('Table1') and b.id=a.cdefault and a.name='Field8' and b.name like 'DF%'
exec('alter table Table1 drop constraint '+@name)
Alter Table Table1 Drop Column Field8
#6
declare @name varchar(8000)
select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('Table1') and b.id=a.cdefault and a.name='Field8' and b.name like 'DF%'
就可以找出 “constraint '默认值约束名'
select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('Table1') and b.id=a.cdefault and a.name='Field8' and b.name like 'DF%'
就可以找出 “constraint '默认值约束名'
#7
怎样可以找出 “constraint '默认值约束名'” ?
呵呵。楼主不仔细看贴。
呵呵。楼主不仔细看贴。
#8
thanks !
有没有通用的SQL语句(支持多个数据库),我现在是在用Java,大家知不知 Java 怎样可以得到 “默认值约束名”?
有没有通用的SQL语句(支持多个数据库),我现在是在用Java,大家知不知 Java 怎样可以得到 “默认值约束名”?
#9
收了
#10
用sql 语句你就别指望跨平台了.
如果用ADOX倒是可以隐藏某些实现细节,只是我不知道java能不能调用adox
如果用ADOX倒是可以隐藏某些实现细节,只是我不知道java能不能调用adox
#11
:)
#12
thanks !