sql查詢表結構,過程,視圖,主鍵,外鍵,約束

來源:互聯網
上載者:User

 一、表結構查詢

SELECT TOP (100) PERCENT a.name AS zdm,COLUMNPROPERTY(a.id, a.name, 'IsIdentity') AS bs ,
CASE WHEN EXISTS (SELECT 1 FROM dbo.sysindexes si INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id
 AND si.indid = sik.indid INNER JOIN dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid
 INNER JOIN dbo.sysobjects so ON so.name = so.name AND so.xtype = 'PK' WHERE sc.id = a.id AND sc.colid = a.colid)
 THEN '1' ELSE '0' END AS zj , b.name AS lx, a.length AS cd, COLUMNPROPERTY(a.id, a.name,'PRECISION')
 AS jd, ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) AS xsws,a.isnullable AS yxk, ISNULL(e.text, '')
  AS mrz, ISNULL(g.value, '') AS zdsm FROM dbo.syscolumns AS a LEFT OUTER JOIN dbo.systypes AS b ON a.xtype = b.xusertype
  INNER JOIN dbo.sysobjects AS d ON a.id = d.id AND d.xtype = 'U' AND d.status >= 0 LEFT OUTER JOIN
   dbo.syscomments AS e ON a.cdefault = e.id LEFT OUTER JOIN sys.extended_properties AS g
   ON a.id = g.major_id AND a.colid = g.minor_id LEFT OUTER JOIN sys.extended_properties
   AS f ON d.id = f.major_id AND f.minor_id = 0  where d .name='查詢的表名'

 

二、
-- 查詢預存程序
select CASE a.xtype  WHEN 'p' THEN '預存程序' end as lx ,a.name, b.text from sysobjects a left outer join syscomments b on a.id = b.id where xtype='p'
--查詢檢視
select  CASE a.xtype  WHEN 'v' THEN '視圖' end as lx,a.name , b.text from sysobjects a left outer join syscomments b on a.id = b.id where xtype='v'

--主鍵,外鍵,約束
select
CASE a.xtype  WHEN 'PK' THEN '主鍵' WHEN 'F' THEN '外鍵' WHEN 'C' THEN '約束'
END AS lx,a.name AS name,
b.text from sysobjects a left outer join syscomments b on a.id = b.id
where (a.xtype IN ( 'C',  'F','PK')) AND
(OBJECTPROPERTY(a.id, N'IsMSShipped') = 0) and a.parent_obj=(select id from sysobjects where name = 'table_2')

 

 

環境是用的sql2008

其中涉及到的表  與視圖  過程的名稱在sql的協助中能夠查到明細

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.