資料庫常用物件查詢(使用者表,所有視圖。。。)

來源:互聯網
上載者:User

感謝csdn網友shmilywcd (天笑(笑揖清風))

原帖http://topic.csdn.net/u/20100729/15/62fd8dee-13ba-4bec-95a8-b14c564fde32.html

--查詢使用者表對象資訊 select Tab.Name as [表名],Tab.create_date as [建立時間],Tab.modify_date as [最後修改時間], Col.Name as [列名] ,Type.name as [資料類型],Col.max_length as [欄位長度], CASE WHEN pk.is_primary_key= 1 THEN 'Y' ELSE 'N' end as [是否主鍵], CASE WHEN Col.is_identity = 1 THEN 'Y' else 'N'end as [是否自增] , identity_columns.seed_value as [自增種子],identity_columns.increment_value as [自增步長], case when Col.is_nullable = 1 then 'Y' else 'N' END AS [是否允許為NULL], Def.text as [預設值],case when Col.is_computed = 1 then 'Y' else 'N' END as [是否計算資料行] , computed_columns.definition as [計算公式],Col_Desc.Value as [列備忘] from sys.objects Tab inner join sys.columns Col on Tab.object_id =Col.object_id inner join sys.types Type on Col.system_type_id = Type.system_type_id left join sys.identity_columns identity_columns on Tab.object_id = identity_columns.object_id and Col.column_id = identity_columns.column_id left join syscomments Def on Col.default_object_id = Def.ID left join( select index_columns.object_id,index_columns.column_id,indexes.is_primary_key from sys.indexes indexes inner join sys.index_columns index_columns on indexes.object_id = index_columns.object_id and indexes.index_id = index_columns.index_id where indexes.is_primary_key = 1/*主鍵*/ ) PK on Tab.object_id = PK.object_id AND Col.column_id = PK.column_id left join sys.computed_columns computed_columns on Tab.object_id =computed_columns.object_id and Col.column_id = computed_columns.column_id left join sys.extended_properties Col_Desc on Col_Desc.major_id=Tab.object_id and Col_Desc.minor_id=Col.Column_id and Col_Desc.class=1 where Tab.type = 'U' and Tab.Name not like'sys%' order by Tab.create_date --查詢所有視圖 select views.Name as [視圖名],Col.Name as [列名] ,Type.name as [資料類型],Col.max_length as [欄位長度] --,Col_Desc.Value as Col_Description from sys.views views inner join sys.columns Col on views.object_id = Col.object_id inner join sys.types Type on Col.system_type_id = Type.system_type_id --left join sys.extended_properties Col_Desc -- on Col_Desc.major_id=views.object_id and Col_Desc.minor_id=Col.Column_id and Col_Desc.class=1 order by Create_Date --查詢外鍵約束 select FK_Name as [外鍵名],Parent_Tab_Name as [外鍵表], [外鍵列]=stuff((select ','+[Parent_Col_Name] from ( select FK.name as FK_Name,Parent_Tab.Name as Parent_Tab_Name,Parent_Col.Name as Parent_Col_Name, Referenced_Tab.Name as Referenced_Tab_Name,Referenced_Col.Name as Referenced_Col_Name from sys.foreign_keys FK inner join sys.foreign_key_columns Col on FK.Object_ID = Col.constraint_object_id inner join sys.objects Parent_Tab ON Col.parent_object_id = Parent_Tab.Object_ID and Parent_Tab.TYPE = 'U' inner join sys.columns Parent_Col on Parent_Tab.Object_ID = Parent_Col.object_id and Col.parent_column_id = Parent_Col.column_id inner join sys.objects Referenced_Tab ON Col.referenced_object_id = Referenced_Tab.Object_ID and Referenced_Tab.TYPE = 'U' inner join sys.columns Referenced_Col on Referenced_Tab.Object_ID = Referenced_Col.object_id and Col.referenced_column_id = Referenced_Col.column_id )t where FK_Name=tb.FK_Name and Parent_Tab_Name = tb.Parent_Tab_Name and Referenced_Tab_Name = tb.Referenced_Tab_Name for xml path('')), 1, 1, ''), Referenced_Tab_Name as [主鍵表], [主鍵列]=stuff((select ','+[Referenced_Col_Name] from ( select FK.name as FK_Name,Parent_Tab.Name as Parent_Tab_Name,Parent_Col.Name as Parent_Col_Name, Referenced_Tab.Name as Referenced_Tab_Name,Referenced_Col.Name as Referenced_Col_Name from sys.foreign_keys FK inner join sys.foreign_key_columns Col on FK.Object_ID = Col.constraint_object_id inner join sys.objects Parent_Tab ON Col.parent_object_id = Parent_Tab.Object_ID and Parent_Tab.TYPE = 'U' inner join sys.columns Parent_Col on Parent_Tab.Object_ID = Parent_Col.object_id and Col.parent_column_id = Parent_Col.column_id inner join sys.objects Referenced_Tab ON Col.referenced_object_id = Referenced_Tab.Object_ID and Referenced_Tab.TYPE = 'U' inner join sys.columns Referenced_Col on Referenced_Tab.Object_ID = Referenced_Col.object_id and Col.referenced_column_id = Referenced_Col.column_id )t where FK_Name=tb.FK_Name and Parent_Tab_Name = tb.Parent_Tab_Name and Referenced_Tab_Name = tb.Referenced_Tab_Name for xml path('')), 1, 1, '') --as [外鍵列] from ( select FK.name as FK_Name,Parent_Tab.Name as Parent_Tab_Name,Parent_Col.Name as Parent_Col_Name, Referenced_Tab.Name as Referenced_Tab_Name,Referenced_Col.Name as Referenced_Col_Name from sys.foreign_keys FK inner join sys.foreign_key_columns Col on FK.Object_ID = Col.constraint_object_id inner join sys.objects Parent_Tab ON Col.parent_object_id = Parent_Tab.Object_ID and Parent_Tab.TYPE = 'U' inner join sys.columns Parent_Col on Parent_Tab.Object_ID = Parent_Col.object_id and Col.parent_column_id = Parent_Col.column_id inner join sys.objects Referenced_Tab ON Col.referenced_object_id = Referenced_Tab.Object_ID and Referenced_Tab.TYPE = 'U' inner join sys.columns Referenced_Col on Referenced_Tab.Object_ID = Referenced_Col.object_id and Col.referenced_column_id = Referenced_Col.column_id )tb group by FK_Name,Parent_Tab_Name,Referenced_Tab_Name --查詢所有預存程序 select Pr_Name as [預存程序], [參數]=stuff((select ','+[Parameter] from ( select Pr.Name as Pr_Name,parameter.name +' ' +Type.Name + ' ('+convert(varchar(32),parameter.max_length)+')' as Parameter from sys.procedures Pr left join sys.parameters parameter on Pr.object_id = parameter.object_id inner join sys.types Type on parameter.system_type_id = Type.system_type_id where type = 'P' ) t where Pr_Name=tb.Pr_Name for xml path('')), 1, 1, '') from ( select Pr.Name as Pr_Name,parameter.name +' ' +Type.Name + ' ('+convert(varchar(32),parameter.max_length)+')' as Parameter from sys.procedures Pr left join sys.parameters parameter on Pr.object_id = parameter.object_id inner join sys.types Type on parameter.system_type_id = Type.system_type_id where type = 'P' )tb where Pr_Name not like 'sp_%' --and Pr_Name not like 'dt%' group by Pr_Name order by Pr_Name --查詢所有觸發器 select triggers.name as [觸發器],tables.name as [表名],triggers.is_disabled as [是否禁用], triggers.is_instead_of_trigger AS [觸發器類型], case when triggers.is_instead_of_trigger = 1 then 'INSTEAD OF' when triggers.is_instead_of_trigger = 0 then 'AFTER' else null end as [觸發器類型描述] from sys.triggers triggers inner join sys.tables tables on triggers.parent_id = tables.object_id where triggers.type ='TR' order by triggers.create_date --查詢所有索引 select indexs.Tab_Name as [表名],indexs.Index_Name as [索引名] ,indexs.[Co_Names] as [索引列], Ind_Attribute.is_primary_key as [是否主鍵],Ind_Attribute.is_unique AS [是否唯一鍵], Ind_Attribute.is_disabled AS [是否禁用] from ( select Tab_Name,Index_Name, [Co_Names]=stuff((select ','+[Co_Name] from ( select tab.Name as Tab_Name,ind.Name as Index_Name,Col.Name as Co_Name from sys.indexes ind inner join sys.tables tab on ind.Object_id = tab.object_id and ind.type in (1,2)/*索引的類型:0=堆/1=聚集/2=非聚集/3=XML*/ inner join sys.index_columns index_columns on tab.object_id = index_columns.object_id and ind.index_id = index_columns.index_id inner join sys.columns Col on tab.object_id = Col.object_id and index_columns.column_id = Col.column_id ) t where Tab_Name=tb.Tab_Name and Index_Name=tb.Index_Name for xml path('')), 1, 1, '') from ( select tab.Name as Tab_Name,ind.Name as Index_Name,Col.Name as Co_Name from sys.indexes ind inner join sys.tables tab on ind.Object_id = tab.object_id and ind.type in (1,2)/*索引的類型:0=堆/1=聚集/2=非聚集/3=XML*/ inner join sys.index_columns index_columns on tab.object_id = index_columns.object_id and ind.index_id = index_columns.index_id inner join sys.columns Col on tab.object_id = Col.object_id and index_columns.column_id = Col.column_id )tb where Tab_Name not like 'sys%' group by Tab_Name,Index_Name ) indexs inner join sys.indexes Ind_Attribute on indexs.Index_Name = Ind_Attribute.name order by indexs.Tab_Name

聯繫我們

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