常春岛资源网 Design By www.syssdc.com

本文向大家介绍了使用SQL语句提取数据库所有表的表名、字段名的实例代码,在SQLserver 中进行了测试,具体内容如下:

--查询所有用户表所有字段的特征
SELECT D.Name as TableName, A.colorder AS ColOrder, A.name AS Name, 
 COLUMNPROPERTY(A.ID,A.Name, 'IsIdentity') AS IsIdentity, 
 CASE WHEN EXISTS
 (SELECT 1
 FROM dbo.sysobjects
 WHERE Xtype = 'PK' AND Name IN
 (SELECT Name
 FROM sysindexes
 WHERE indid IN
 (SELECT indid
 FROM sysindexkeys
 WHERE ID = A.ID AND colid = A.colid))) 
 THEN 1 ELSE 0 END AS 主键, 
 B.name AS 类型,
 A.length AS [长度],
 A.xprec AS [精度],
 A.xscale AS [小数],
 CASE WHEN A.isnullable = 1 THEN 1 ELSE 0 END AS [可为空], 
ISNULL(E.text, ' ') AS [默认值], 
 ISNULL(G.[value], ' ') AS [说明]
FROM dbo.syscolumns A LEFT OUTER JOIN
 dbo.systypes B ON A.xtype = B.xusertype INNER JOIN
 dbo.sysobjects D ON A.id = D.id AND D.xtype = 'U' AND 
 D.name <> 'dtproperties' LEFT OUTER JOIN
 dbo.syscomments E ON A.cdefault = E.id 
 LEFT OUTER JOIN dbo.sysproperties G ON A.id = G.id AND A.colid = G.smallid
--WHERE D.Name='tablename' --如果找指定表,把注释去掉
ORDER BY 1, 2

------在SQLserver 2005中测试

--查询所有用户表所有字段的特征
SELECT  
(case when a.colorder=1 then d.name else '' end) as 表名,--如果表名相同就返回空  
   a.colorder as 字段序号,  
   a.name as 字段名,  
   (case when COLUMNPROPERTY( a.id,a.name, 'IsIdentity' )=1 then '√' else '' end) as 标识,  
   (case when (SELECT count(*) FROM sysobjects--查询主键  
           WHERE (name in  
               (SELECT name FROM sysindexes  
               WHERE (id = a.id)  AND (indid in  
                   (SELECT indid FROM sysindexkeys  
                    WHERE (id = a.id) AND (colid in  
                     (SELECT colid FROM syscolumns  
                     WHERE (id = a.id) AND (name = a.name))  
             )))))  
     AND (xtype = 'PK' ))>0 then '√' else '' end) as 主键,--查询主键END  
b.name as 类型,  
a.length as 占用字节数,  
COLUMNPROPERTY(a.id,a.name,'PRECISION' ) as  长度,  
isnull(COLUMNPROPERTY(a.id,a.name,'Scale' ),0) as 小数位数,  
(case when a.isnullable=1 then '√' else '' end) as 允许空,  
isnull(e.text,'' ) as 默认值,  
isnull(g.[value],'' ) AS 字段说明  
FROM syscolumns a left join systypes b  
on a.xtype=b.xusertype  
inner join sysobjects d  
on a.id=d.id and d.xtype='U' and d.name<> 'dtproperties'  
left join syscomments e  
on a.cdefault=e.id  
left join sys.extended_properties g  
on a.id=g.major_id AND a.colid = g.minor_id  
   --where d.name='PE_U_ValliMessage' --所要查询的表  
order by a.id,a.colorder

------提取某个表的字段名

SELECT
(case when a.colorder=1 then d.name else '' end) N'表名',
a.colorder N'字段序号',
a.name N'字段名',
isnull(g.[value],'') AS N'字段说明',
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else ''
end) N'标识',
(case when (SELECT count(*)
FROM sysobjects
WHERE (name in
(SELECT name
FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid
FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid
FROM syscolumns
WHERE (id = a.id) AND (name = a.name))))))) AND
(xtype = 'PK'))>0 then '√' else '' end) N'主键',
b.name N'类型',
a.length N'占用字节数',
COLUMNPROPERTY(a.id,a.name,'PRECISION') as N'长度',
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as N'小数位数',
(case when a.isnullable=1 then '√'else '' end) N'允许空',
isnull(e.text,'') N'默认值'

FROM syscolumns a
left join systypes b
on a.xtype=b.xusertype
inner join sysobjects d
on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e
on a.cdefault=e.id
left join sys.extended_properties g
on a.id=g.major_id AND a.colid = g.minor_id
--where条件输入表名
where d.name='ConsumeBillBody'
order by object_name(a.id),a.colorder

总结

以上就是本文关于SQL提取数据库表名及字段名等信息代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:SQL注入原理与解决方法代码示例、MySQL删除有外键约束的表数据方法介绍、几个比较重要的MySQL变量等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!

标签:
sql,获取表名,sql,获取表的字段名

常春岛资源网 Design By www.syssdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
常春岛资源网 Design By www.syssdc.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。