sap-ase - 如果存在非空或默认值的更改表语句不起作用

标签 sap-ase alter-table

我尝试添加一个新列 第2列 test_tbl 并将列设置为默认值 “不适用”不为空 .声明如下:

if not exists (select 1 from syscolumns where object_name(id) = 'test_tbl' and name = 'column2')
begin
  alter table test_tbl add column2 varchar(20) default 'N/A' not null
end

错误是
Could not execute statement.
Column names in each table must be unique. Column name 'column2' in table 'test_tbl' is specified more than once.
Sybase error code=2705
Severity Level=16, State=3, Transaction State=1
Line 4

但是如果我添加一列 可以为空 .
if not exists (select 1 from syscolumns where object_name(id) = 'test_tbl' and name = 'column2')
begin
    alter table test_tbl add column2 varchar(20) null
end

它可以工作。我对这些很困惑。
我搜索了一些标签,知道动态 sql 可以工作。

The error is being raised during normalizations (as the parse tree is being converted into a normalized query tree) rather than at execution. The contents of dynamic sql aren't processed until they are actually called, avoiding the error.



在 Sybase DOC 中关于 if...else

When an alter table, create table, or create view command occurs within an if...else block, Adaptive Server creates the schema for the table or view before determining whether the condition is true. This may lead to errors if the table or view already exists.



我想知道为什么可空列语句可以无错误地执行!

最佳答案

我在 Sybase ASE 15 上看到了相同的行为

除了您已经从 Sybase Documentaiton 中引用的内容之外,我无法为您提供解释,但是您可以通过将调用 alter table 包装在 execute() 语句中来编写一致的行为,如下所示

if not exists (select 1 from syscolumns where object_name(id) = 'tbl_test' and name = 'column2')
begin     
    execute("
        alter table tbl_test add column2 varchar(20) default 'N/A' not null
    ")
end

我的猜测是服务器能够评估 if...else在执行alter 语句之前在此实例中使用语句。

关于sap-ase - 如果存在非空或默认值的更改表语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11357255/

相关文章:

MySQL远程: Alter privilege granted,但不工作

database - sybase系统程序-如何得到结果?

sql - Sybase *= 到 Ansi 标准,同一个内表有 2 个不同的外表

sqlite - 在执行 SQLite alter table 技巧时更新外键引用

php - MySQL DDL 触发器,用于列重命名的差异表模式

sql - 在 ALTER TABLE (SQL) 中,没有参数的简单 MODIFY 会改变什么?

database - 如何在 ABAP 中将一个大数据库表复制到另一个数据库表?

java - 从Sybase ASE 15.7数据库读取存储过程源代码

sap-ase - 我需要 Sybase 数据类型来保存不确定长度的字符串。

mysql - 2列在mysql表中是唯一的