好的,我知道,这是一个非常老的话题 - 但我在其他地方找不到足够的答案,所以我以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)。这就是为什么我仍然添加了column status。对于identity(位7),一切都按预期工作。如果有任何解释,将非常感激。
- 使用isql时,请从足够宽度开始(例如-w160)
- syscolumns.name和systypes.name的默认列宽度非常大,因此我正在使用substring(....)。如果您的列名不适合,请调整复制字符的数量(substring()的最后一个参数)。
- 通过省略表名中的'tempdb..',该查询在使用sp_xxx命令之前,也可用于普通非tempdb表。
SELECT name FROM tempdb..syscolumns WHERE id = OBJECT_ID('tempdb..#mytable')
,因为OP说“在Sybase上它返回空”。 - Martin Smith