日期:2014-05-17 浏览次数:20796 次
create table test001 (
id int IDENTITY(1,1),
a int ,
b numeric(10,2),
c as a*b
)
go
select name,case when colstat &1=1 then '标志列' when colstat &4=4 then '计算列' else '一般列' end as 属性 from syscolumns
where id=object_id('test001')
--结果
name 属性
-------------------------------------------------------- ------
id 标志列
a 一般列
b 一般列
c 计算列
(所影响的行数为 4 行)
------解决方案--------------------
--> 2000中模拟2005的sys.identity_columns
select
object_name(id) as table_name,
ident_seed(object_name(id)) as seed_value,
ident_incr(object_name(id)) as increment_value,
ident_current(object_name(id)) as last_value,
*
from syscolumns
where objectproperty(id, 'IsUserTable') = 1 and status = 0x80
--> 2000中模拟2005的sys.computed_columns
select
object_name(a.id) as table_name,
b.text as definition,
a.*
from syscolumns a join syscomments b on a.id = b.id
where objectproperty(a.id, 'IsUserTable') = 1 and a.iscomputed = 1
------解决方案--------------------
create table T(ID int identity,Col1 int,Col2 as ID+COl1)
go
select
Name as 列名,
case when iscomputed=1 then '计算列' when status=0x80 then '标识列' else '一般列' end as 属性
from
syscolumns
where
ID=object_id('T')
列名 属性
-------------------------------------------------------- ------
ID 标识列
Col1 一般列
Col2 计算列
(所影响的行数为 3 行)
------解决方案--------------------
/*
谢谢1楼和3楼以及2楼精神上的支持,那如何得到计算列的定义呢?比如在三楼举的例子中如何知道列b是被定义为numeric(10,2)的呢?
*/
--> 参考。未尽之处自己改或加 when...
select
table_name = object_name(a.id),
column_name = a.name,
data_type_definition = b.name +
case
when b.name in ('char', 'varchar', 'binary','varbinary') then '('+ltrim(a.length)+')'
when b.name in ('nchar', 'nvarchar') then '('+ltrim(a.length/2)+')'
when b.name in ('decimal','numeric','float','real') then '('+ltrim(a.xp