获取SqlServer2005表结构(字段,主键,外键,递增,描述)(轉)

    看見"http://www.cnblogs.com/eflylab/archive/2008/06/23/1227838.html"頁面寫的SQL,自己照著做了一下。

1、获取表的基本字段属性
--获取SqlServer中表结构 
select sc.name,st.name,sc.isnullable,sc.length 
from syscolumns sc,systypes st
where sc.xusertype=st.xusertype
and sc.id=object_id('表名')

2、获取字段的描述信息则
--获取SqlServer中表结构 主键,及描述
declare @table_name as varchar(max)
set @table_name = '表名'
select scol.name, stp.name, scol.max_length, scol.is_nullable,
(
    
select count(*)
    
from sys.identity_columns icol,sys.columns col
    
where icol.object_id=col.object_id
    
and  col.column_id=icol.column_id
as is_identity,
(
    
select value
    
from sys.extended_properties ep,sys.columns col
    
where ep.major_id=col.object_id
    
and ep.minor_id=col.column_id
as description
from sys.columns scol,sys.tables stab,sys.types stp
where scol.object_id=stab.object_id
and scol.system_type_id=stp.system_type_id
and stab.name=@table_name
order by scol.column_id

3、获取表的主外键
--获取表主外键约束
exec sp_helpconstraint '表名' ;

4、单独查询表的递增字段
--单独查询表递增字段
select [name] from syscolumns where 
id
=object_id(N'你的表名'and COLUMNPROPERTY(id,name,'IsIdentity')=1





5、OBJECT_ID用法
    作用:返回架构范围内对象的数据库对象标识号。

要使用的对象。object_name 的数据类型为 varcharnvarchar。如果 object_name 的数据类型为 varchar,则它将隐式转换为 nvarchar。可以选择是否指定数据库和架构名称。

    ' object_type '

架构范围的对象类型。object_type 的数据类型为 varcharnvarchar。如果 object_type 的数据类型为 varchar,则它将隐式转换为 nvarchar


 5.5 示例

    以下示例返回 AdventureWorks 数据库中 Production.WorkOrder 表的对象 ID。

SELECT OBJECT_ID(N'AdventureWorks.Production.WorkOrder'AS 'Object ID';



6、 object_id(N'orders')中N的作用
     这个数据类型是启用Unicode,如果字符串里包含中文、日文等unicode大字符集的字符,字符串自动在内部使用unicode格式(一个字符占两个字节)。
原文地址:https://www.cnblogs.com/scottckt/p/1228406.html