加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 15:22:07 所属栏目:MsSql教程 来源:网络整理
导读: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、获取字段的描述信息则 -- 获取SqlSer

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用法
??? 作用:返回架构范围内对象的数据库对象标识号。

? 5.1 语法
?OBJECT_ID ( '[ database_name . [ schema_name ] .?| schema_name . ]?????object_name'?[,'object_type' ] ) ? 5.2 参数
????? '? object_name? '

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

??? '? object_type? '

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

? 5.3 返回类型
? ? int
? 5.4 备注
???? 当该参数对系统函数可选时,则系统采用当前数据库、主机、服务器用户或数据库用户。内置函数后面必须跟圆括号。当指定临时表名时,除非当前数据库为? tempdb,否则必须在该临时表名之前加上数据库名称。
??? 例如: SELECT OBJECT_ID('tempdb..#mytemptable')

?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格式(一个字符占两个字节)。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读