首页 > 解决方案 > 如何在 Sybase ASE 中查找列的默认值

问题描述

我们想在 Sybase ASE 中找到列的默认值。下面给出了 SQL 服务器的类似查询,我们希望 sybase 也一样。

SELECT COLUMN_DEFAULT  from INFORMATION_SCHEMA.COLUMNS 

标签: sap-ase

解决方案


注意:从这个相关的问答中复制:


如何在 Sybase ASE 系统表中查找默认值...

  • 如果列具有默认值,则syscolumns 中的关联记录将具有存储在cdefault列 中的非零值,即 syscolumns.cdefault > 0
  • 非零cdefault值是指向syscomments中存储有关默认信息的记录的指针,即syscolumns.cdefault = syscomments.id
  • 实际默认值以两种格式之一存储在syscomments.text列中,具体取决于默认值是a)内联定义为create/alter table命令的一部分,还是b)通过create default命令定义为独立默认值。

例子:

-- inline default value of '0' assigned to column 'b'
create table t1 (a int, b int default 0 not null, c int)
go

-- standalone default named 'myzero' with default value of '0'; bound to column 'c'
create default myzero as 0
go
sp_bindefault myzero, 't1.c'
go

sp_help t1
go
...
 Column_name ... Default_name
 ----------- ... --------------
 a           ... NULL          
 b           ... t1_b_467529718             -- inline default
 c           ... myzero                     -- standalone default
...

-- contents of syscolumns.cdefault
select name, cdefault from syscolumns where id = object_id('t1') order by colid
go
 name       cdefault
 ---------- -----------
 a                    0
 b            467529718
 c            387529433

-- contents of syscomments.text
select id,text from syscomments where id in (467529718,387529433) order by id
go
 id        text
 --------- ---------------------------
 467529718 DEFAULT  0                       -- inline default
 387529433 create default myzero as 0       -- standalone default

-- pulling this all together:

select col.name, com.text
from   syscolumns  col,
       syscomments com
where  col.id = object_id('t1')
and    col.cdefault = com.id
order by 1
go
 name       text
 ---------- ------------------------------
 b          DEFAULT  0                      -- inline default
 c          create default myzero as 0      -- standalone default

从这里您应该能够根据syscomments.text列的格式和实际的默认值(即,它是数字吗?它是一个字符串,可能带有嵌入的空格?它是一个函数参考)解析出默认值,例如getdate() )。


推荐阅读