【问题标题】:Sybase ASE identify columns of keys of multiple tablesSybase ASE 识别多个表的键列
【发布时间】:2012-01-23 20:27:41
【问题描述】:

我正在尝试识别 ASE 中组成键的列。

Sybase 在此处列出了解决方案:http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.help.ase.15.5/title.htm

我在下面有一个稍微修改过的版本,但是它只有在我查找单个表时才有效(就像 sybase 的解决方案一样),但我想使用 'in' 关键字并一次性查找所有表。

我可以得到一些帮助,为什么下面的解决方案不起作用?它只为“t5”表生成列列表。

declare @keycnt integer
declare @objname varchar(256)
select @keycnt = keycnt, @objname = sysobjects.name from sysindexes, sysobjects
     where 
     --sysobjects.id = object_id("t5")
     --sysobjects.id = object_id("t4")
     sysobjects.id in (object_id("t5"), object_id("t4"))
     and sysobjects.id = sysindexes.id
     and indid = 1
while @keycnt > 0
begin
     select index_col(@objname, 1, @keycnt)
     select @keycnt = @keycnt - 1
end

这些是我用于测试的表:

CREATE TABLE t4(
    [value] [varchar] (500) not NULL ,
  CONSTRAINT pk_g4 PRIMARY KEY CLUSTERED (
    [value]
  )
)

CREATE TABLE t5(
    [myvalue] [varchar] (500) not NULL ,
  CONSTRAINT pk_g4 PRIMARY KEY CLUSTERED (
    [myvalue]
  )
)

【问题讨论】:

    标签: database sap-ase


    【解决方案1】:

    您有两种解决方案:

    使用 OR

    declare @keycnt integer
    declare @objname varchar(256)
    select @keycnt = keycnt, @objname = sysobjects.name from sysindexes, sysobjects
         where 
         --sysobjects.id = object_id("t5")
         --sysobjects.id = object_id("t4")
         (sysobjects.id = object_id("t5") OR sysobjects.id = object_id("t4"))
         and sysobjects.id = sysindexes.id
         and indid = 1
    while @keycnt > 0
    begin
         select index_col(@objname, 1, @keycnt)
         select @keycnt = @keycnt - 1
    end
    

    或者使用动态SQL来正确使用IN。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多