Export SQL server data dictionary statements (favorites)

Source: Internet
Author: User
Tags scalar

1. Export the table:

Select top (1, 100) percent
Case when a. colorder = 1 then D. Name else ''end as table name, case when a. colorder = 1 then isnull (F. Value ,'')
Else ''end as table description, A. colorder as field serial number, A. Name as field name, case when columnproperty (A. ID, A. Name, 'isidentity ')
= 1 then '√ 'else' end as ID, 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 =. ID and SC. colid =. colid) Then '√ 'else' 'end as primary key, B. name as type,. length as length, columnproperty (. ID,. name,
'Precision ') as precision, isnull (columnproperty (A. ID, A. Name, 'Scale'), 0) as decimal places,
Case when. isnullable = 1 then' √ 'else' end as can be null, isnull (E. text, '') as default value, isnull (G. value, '') as field description, D. crdate as creation time,
Case when a. colorder = 1 then D. refdate else null end as change time
From DBO. syscolumns as a left Outer Join
DBO. policypes 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
Order by D. Name, field serial number

2 \ import index
Select Top 100 percent -- A. ID,
Case when B. keyno = 1 then C. Name else ''end as table name,
Case when B. keyno = 1 then A. Name else ''end as index name, D. Name as column name,
B. keyno as index order, case indexkey_property (C. ID, B. indid, B. keyno, 'isdesending ')
When 1 then 'descending order 'when 0 then' Ascending Order 'end as sorting, case when P. ID is null
Then ''else' √ 'end as primary key, Case indexproperty (C. ID, A. Name, 'isclustered ')
When 1 then '√ 'when 0 then'' end as aggregation, Case indexproperty (C. ID,
A. Name, 'isunique') when 1 then' √ 'when 0 then'' end as unique,
Case when e. ID is null then ''else' √ 'end as unique constraint,
A. origfillfactor as fill factor, C. crdate as creation time, C. refdate as change time
From DBO. sysindexes a inner join
DBO. sysindexkeys B on A. ID = B. ID and A. indid = B. indid inner join
DBO. syscolumns D on B. ID = D. id and B. colid = D. colid inner join
DBO. sysobjects C on A. ID = C. ID and C. xtype = 'U' left Outer Join
DBO. sysobjects e on E. Name = A. Name and E. xtype = 'uq' left Outer Join
DBO. sysobjects P on p. Name = A. Name and P. xtype = 'pk'
Where (objectproperty (A. ID, N 'isusertable') = 1) and (objectproperty (A. ID,
N 'ismsshipped ') = 0) and (indexproperty (A. ID, A. Name, 'isautostatistics') = 0)
Order by C. Name, A. Name, B. keyno

3 \ export primary \ foreign key \ constraint \ attempt and other objects
Select distinct
Top 100 percent o. xtype,
Case O. xtype when 'X' extended stored procedure 'when' tr'then' trigger 'when' pK' then
The 'when' f'then' foreign key 'when 'C' constraint 'when' v 'then' view 'when' FN'
Then' function-scalar 'when' if 'then' function-nested 'when' TF 'then' function-Table value 'else' Stored Procedure'
End as type, O. Name as object name, O. crdate as creation time, O. refdate as change time,
C. Text as statement
From DBO. sysobjects o left Outer Join
DBO. syscomments C on O. ID = C. ID
Where (O. xtype in ('x', 'tr', 'C', 'V', 'F', 'if', 'tf', 'fn, 'P ', 'pk') and
(Objectproperty (O. ID, n' ismsshipped ') = 0)
Order by case o. xtype when 'X' then' extended stored procedure 'when' tr 'then' trigger 'when
'Pk 'then' primary key 'when' f'then' foreign key 'when'c' then' constraint 'when' v'then' view'
When 'fn 'then' function-scalar 'when' if 'then' function-nested 'when' TF 'then' function-Table value'
Else 'stored process' end DESC

 

4. Export index fragmentation Rate

Declare @ db_name varchar (256)
Set @ db_name = 'mydb' -- Database Name
Select db_name (A. database_id) [db_name], C. name [table_name], B. name [index_name], A. avg_fragmentation_in_percent
From SYS. dm_db_index_physical_stats (db_id (@ db_name), null, 'limited') as
Join SYS. indexes as B on A. object_id = B. object_id
And a. index_id = B. index_id
Join SYS. Tables as C on A. object_id = C. object_id
Where a. index_id> 0
And a. avg_fragmentation_in_percent> 50

5 triggers

Select a. Name as [Table], B. Name as [Trigger]
From sysobjects A, sysobjects B
Where a. ID = B. parent_obj
And a. xtype = 'U' and B. xtype = 'tr'

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.