【问题标题】:Sybase - Default ConstraintsSybase - 默认约束
【发布时间】:2011-05-25 05:52:06
【问题描述】:

我需要在 Sybase 中删除表上的所有默认列级约束。

我不知道该怎么做,我曾尝试通过以下方式禁用约束:

ALTER TABLE Employee NOCHECK CONSTRAINT ALL

上面的甚至不起作用,给出如下错误:

Error (156) Incorrect syntax near the keyword 'CONSTRAINT'

另外,我尝试了一些自定义存储过程,使用 sys 表,但不符合 Sybase 语法,它适用于 SQL 服务器,如下所示:

declare @sql varchar(1024)
declare curs cursor for
select 'ALTER TABLE '+tab.name+' DROP CONSTRAINT '+cons.name
from sysobjects cons,sysobjects tab
where cons.type in ('D')
  and cons.parent_object_id=tab.object_id and tab.type='U'
order by cons.type

open curs
fetch next from curs into @sql
while (@@fetch_status = 0)
begin
 exec(@sql)
 fetch next from curs into @sql
end
close curs
deallocate curs

谁能解开这个谜题..

【问题讨论】:

    标签: sybase


    【解决方案1】:

    我自己对 SQL 不是很熟悉,但是否必须仅使用 SQL 来解决这个问题?您还可以编写一个脚本,在运行时获取模式并找出给定表上所有约束的名称。例如,

    sp_helpconstraint $tableName
    

    接下来,您可以使用以下命令:

    alter table table_name 
     drop constraint constraint_name
    

    有关更多详细信息,请参阅以下参考资料:

    sp_helpconstraint

    Dropping columns or column constraints

    【讨论】:

      【解决方案2】:

      在 sybase ASE 中,如果要删除表上的默认约束,那么

      找到该默认约束的约束名称为

      sp_helpconstraint 表名

      现在执行这个查询

      alter table TableName 删除约束约束名

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-11
        • 2012-06-18
        • 1970-01-01
        • 1970-01-01
        • 2011-08-17
        • 2016-07-08
        • 2021-04-26
        相关资源
        最近更新 更多