擷取sql server資料庫表結構

來源:互聯網
上載者:User

標籤:

if exists (select 1 from sysobjects where name = ‘sysproperties‘and xtype = ‘V‘)
begin
    DROP VIEW sysproperties
end
    GO
    CREATE VIEW sysproperties
AS
 SELECT class AS id,Minor_id AS sMallid,* from sys.extended_properties 
 

-------在sql server 2005中擷取表結構請先執行以上語句


SELECT
  表名   = CASE a.colorder WHEN 1 THEN c.name ELSE ‘‘ END,
  序     = a.colorder,
  欄位名 = a.name,
  標識   = CASE COLUMNPROPERTY(a.id,a.name,‘IsIdentity‘) WHEN 1 THEN ‘√‘ ELSE ‘‘ END,
  主鍵   = CASE
    WHEN EXISTS (
      SELECT *
      FROM sysobjects
      WHERE xtype=‘PK‘ AND 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
          )
        )
      )
    )
    THEN ‘√‘
    ELSE ‘‘
  END,
  類型   = b.name,
  位元組數 = a.length,
  長度   = COLUMNPROPERTY(a.id,a.name,‘Precision‘),
  小數   = CASE ISNULL(COLUMNPROPERTY(a.id,a.name,‘Scale‘),0)
    WHEN 0 THEN ‘‘
    ELSE CAST(COLUMNPROPERTY(a.id,a.name,‘Scale‘) AS VARCHAR)
  END,
  允許空 = CASE a.isnullable WHEN 1 THEN ‘√‘ ELSE ‘‘ END,
  預設值 = ISNULL(d.[text],‘‘),
  說明   = ISNULL(e.[value],‘‘)
FROM syscolumns a
  LEFT  JOIN systypes      b ON a.xtype=b.xusertype
  INNER JOIN sysobjects    c ON a.id=c.id AND c.xtype=‘U‘ AND c.name<>‘dtproperties‘ and c.name=‘此處改為你擷取的表名稱‘
  LEFT  JOIN syscomments   d ON a.cdefault=d.id
  LEFT  JOIN sysproperties e ON a.id=e.id AND a.colid=e.smallid
ORDER BY c.name, a.colorder

 

擷取sql server資料庫表結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.