好吧,我知道了,很老的话题 - 但我无法找到足够的答案其他地方,所以我自己的研究与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文档导出,但不知何故我第3位在tempdb..syscolumns中的状态不会相应地改变,请参阅我的示例中的列num。这就是为什么我添加列状态的原因。对于身份(位7),事情按预期工作。任何解释将非常感激。 (例如-w160)
- syscolumns.name和systypes.name的默认列宽很大,因此我使用子字符串(....)。如果列名不适合,请调整复制的字符数(substring()的最后一个参数)。
- 通过从表名中省略'tempdb ..',此查询也适用于普通非tempdb表,以防止查询优先于使用sp_xxx命令。
你试过用OBJECT_ID('tempdb ..#mytable')搜索吗? – 2011-05-09 21:22:00
@Andriy - 我删除了我的答案,提示'SELECT name FROM tempdb..syscolumns WHERE id = OBJECT_ID('tempdb ..#mytable')',因为OP说“它在Sybase上什么也不会返回” – 2011-05-09 21:46:06