真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

SQL提取數(shù)據(jù)庫表名及字段名等信息代碼示例

本文向大家介紹了使用SQL語句提取數(shù)據(jù)庫所有表的表名、字段名的實(shí)例代碼,在SQLserver 中進(jìn)行了測(cè)試,具體內(nèi)容如下:

建網(wǎng)站原本是網(wǎng)站策劃師、網(wǎng)絡(luò)程序員、網(wǎng)頁設(shè)計(jì)師等,應(yīng)用各種網(wǎng)絡(luò)程序開發(fā)技術(shù)和網(wǎng)頁設(shè)計(jì)技術(shù)配合操作的協(xié)同工作。成都創(chuàng)新互聯(lián)公司專業(yè)提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì),網(wǎng)頁設(shè)計(jì),網(wǎng)站制作(企業(yè)站、響應(yīng)式網(wǎng)站建設(shè)、電商門戶網(wǎng)站)等服務(wù),從網(wǎng)站深度策劃、搜索引擎友好度優(yōu)化到用戶體驗(yàn)的提升,我們力求做到極致!

--查詢所有用戶表所有字段的特征
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 [長(zhǎng)度],
 A.xprec AS [精度],
 A.xscale AS [小數(shù)],
 CASE WHEN A.isnullable = 1 THEN 1 ELSE 0 END AS [可為空], 
ISNULL(E.text, ' ') AS [默認(rèn)值], 
 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中測(cè)試

--查詢所有用戶表所有字段的特征
SELECT  
(case when a.colorder=1 then d.name else '' end) as 表名,--如果表名相同就返回空  
   a.colorder as 字段序號(hào),  
   a.name as 字段名,  
   (case when COLUMNPROPERTY( a.id,a.name, 'IsIdentity' )=1 then '√' else '' end) as 標(biāo)識(shí),  
   (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 占用字節(jié)數(shù),  
COLUMNPROPERTY(a.id,a.name,'PRECISION' ) as  長(zhǎng)度,  
isnull(COLUMNPROPERTY(a.id,a.name,'Scale' ),0) as 小數(shù)位數(shù),  
(case when a.isnullable=1 then '√' else '' end) as 允許空,  
isnull(e.text,'' ) as 默認(rèn)值,  
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

------提取某個(gè)表的字段名

SELECT
(case when a.colorder=1 then d.name else '' end) N'表名',
a.colorder N'字段序號(hào)',
a.name N'字段名',
isnull(g.[value],'') AS N'字段說明',
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else ''
end) N'標(biāo)識(shí)',
(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'占用字節(jié)數(shù)',
COLUMNPROPERTY(a.id,a.name,'PRECISION') as N'長(zhǎng)度',
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as N'小數(shù)位數(shù)',
(case when a.isnullable=1 then '√'else '' end) N'允許空',
isnull(e.text,'') N'默認(rè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

總結(jié)

以上就是本文關(guān)于SQL提取數(shù)據(jù)庫表名及字段名等信息代碼示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:SQL注入原理與解決方法代碼示例、MySQL刪除有外鍵約束的表數(shù)據(jù)方法介紹、幾個(gè)比較重要的MySQL變量等,有什么問題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。感謝朋友們對(duì)本站的支持!


新聞標(biāo)題:SQL提取數(shù)據(jù)庫表名及字段名等信息代碼示例
當(dāng)前URL:http://weahome.cn/article/gocgij.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部