【问题标题】:get the columns of a Temp table in sybase在sybase中获取临时表的列
【发布时间】:2011-08-21 17:20:29
【问题描述】:

有没有办法在 sybase 中获取临时表的列列表? 假设我有一个名为#mytable 的表

select count (*) from  tempdb..#mytable

返回 145 表示此表中有 145 行。 我尝试了以下(有一些变化)

select so.name from tempdb..syscolumns sc inner join tempdb..sysobjects so on sc.id = so.id where so.name = '#mytable'

也试过

select so.name from tempdb..syscolumns sc inner join tempdb..sysobjects so on sc.id = so.id where so.name = 'tempdb..#mytable'

两者都返回空结果。

有什么想法吗?任何其他原语来获取 sybase 中临时表的列名?

【问题讨论】:

  • 您是否尝试按 OBJECT_ID('tempdb..#mytable') 进行搜索?
  • @Andriy - 我删除了建议 SELECT name FROM tempdb..syscolumns WHERE id = OBJECT_ID('tempdb..#mytable') 的答案,因为 OP 说“它在 Sybase 上没有返回任何内容”

标签: sql sybase temp-tables


【解决方案1】:

我很抱歉,但我没有 Sybase 可以试用。但是,我可以给您我认为的答案,但您可能需要做一些工作才能使语法正确。基本上,根据文档,只要您从tempdb 执行此操作,您就可以在临时表上使用sp_help 命令。以下是 Sybase 的引述:

只有从 tempdb 调用系统过程(如 sp_help)才能对临时表起作用。

Reference

以下是 sp_help 命令的使用方法:

http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.sprocs/html/sprocs/sprocs118.htm

OBJECT_ID(tempdb..#mytable) 命令不起作用的原因是该表中不存在该表名。原因是 Sybase 确保所有临时表都是唯一的。为此,它采用临时表名称(包括井号)并将其截断为 13 个字符,附加下划线使其为 13 个字符(以防它很短),并将 17 位会话 ID 添加到表名的结尾。这样,您可以拥有一个名为 #mytable 的临时表,并且另一个用户(甚至您在另一个会话中)可以为临时表使用完全相同的名称,而不会引起冲突。如果你知道你的会话 ID,你可能会建立你的临时表名。如果您构建了临时表名称,您可以将其分配给一个变量(比如@newTableName)并使用SELECT name FROM tempdb..syscolumns WHERE id = OBJECT_ID(@newTableName) 方法来检索您的临时表列。

【讨论】:

    【解决方案2】:

    好的,我知道,非常老的话题 - 但我在其他任何地方都找不到足够的答案,所以我以 IAmTimCorey 的答案为起点进行了研究。这给出了以下结果:

    SELECT sc.colid,
           Substring(sc.NAME, 1, 40) 'column name',
           Substring(st.NAME, 1, 40) 'type',
           sc.length,
           sc.prec,
           sc.status,
           ( CASE
               WHEN ( sc.status & 8 ) != 0 THEN 'Y'
               ELSE 'N'
             END )                   AS 'nullable',
           ( CASE
               WHEN ( sc.status & 128 ) != 0 THEN 'Y'
               ELSE 'N'
             END )                   AS 'identity'
    FROM   tempdb..syscolumns sc
           INNER JOIN tempdb..sysobjects so
                   ON sc.id = so.id
           INNER JOIN systypes st
                   ON st.type = sc.type
                      AND st.usertype = sc.usertype
    WHERE  so.NAME = 'test'
    ORDER  BY sc.colid
    

    例子:

    1> create table tempdb..test(id numeric (15,0) identity, string varchar(40), num  numeric(15,0) not null, dt datetime, flt float)
    2> go
    1> select sc.colid, substring(sc.name, 1, 40) 'column name', substring(st.name, 1, 40) 'type', sc.length, sc.prec, sc.status, (case when (sc.status & 8) != 0 then 'Y' else 'N' end) as nullable, (case when (sc.status & 128) != 0 then 'Y' else 'N' end) as ident from tempdb..syscolumns sc inner join tempdb..sysobjects so on sc.id = so.id inner join systypes st on st.type = sc.type and st.usertype = sc.usertype where so.name = 'test' order by sc.colid
    2> go
     colid  column name                              type                                     length      prec status nullable ident
     ------ ---------------------------------------- ---------------------------------------- ----------- ---- ------ -------- -----
          1 id                                       numeric                                            8   15    128 N        Y
          2 string                                   varchar                                           40 NULL      0 N        N
          3 num                                      numeric                                            8   15      0 N        N
          4 dt                                       datetime                                           8 NULL      0 N        N
          5 flt                                      float                                              8 NULL      0 N        N
    
    (5 rows affected)
    1>
    

    备注:

    • 可空列的检测源自 Sybase 文档,但由于我不知道的原因,tempdb..syscolumns 中状态的第 3 位没有相应更改,请参见示例中的第 num 列。这就是为什么我无论如何都添加了列状态。对于身份(第 7 位),事情按预期工作。任何解释将不胜感激。
    • 使用 isql 时,以足够的宽度开始(例如 -w160)
    • syscolumns.name 和 systypes.name 的默认列宽非常大,因此我使用的是 substring(....)。如果您的列名不合适,请调整复制的字符数(substring() 的最后一个参数)。
    • 通过从表名中省略“tempdb..”,此查询也适用于普通的非 tempdb 表,以防查询优于使用 sp_xxx 命令。

    【讨论】:

      【解决方案3】:

      试试这个

      select sc.id, sc.number, sc.name from tempdb..syscolumns sc inner join tempdb..sysobjects so on sc.id = so.id where so.name like '%mytable%'
      

      在您的选择中没有 # 或任何其他 temp..#mytable 引用。

      【讨论】:

      • 为什么这能解决问题?
      猜你喜欢
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多