批量導出索引的代碼:
WITH indexInfo as ( SELECT SCHEMA_NAME(t.schema_id) [schema_name],t.name as [table_name],t1.name as [index_name] ,t1.type,t1.type_desc,t1.is_unique,t1.is_primary_key,t1.is_unique_constraint,t1.has_filter,t1.filter_definition ,STUFF((SELECT ','+t4.name FROM sys.sysindexkeys t2 inner join sys.index_columns t3 ON t2.id=t3.object_id and t2.indid=t3.index_id and t2.colid=t3.column_id inner join sys.syscolumns t4 ON t2.id=t4.id and t2.colid=t4.colid WHERE t2.id=t1.object_id and t1.index_id=t2.indid and t2.keyno <> 0 ORDER BY t3.key_ordinal FOR XML PATH('')),1,1,'') AS index_cols ,STUFF((SELECT ','+t4.name FROM sys.sysindexkeys t2 inner join sys.index_columns t3 ON t2.id=t3.object_id and t2.indid=t3.index_id and t2.colid=t3.column_id inner join sys.syscolumns t4 ON t2.id=t4.id and t2.colid=t4.colid WHERE t2.id=t1.object_id and t1.index_id=t2.indid and t2.keyno = 0 ORDER BY t3.key_ordinal FOR XML PATH('')),1,1,'') AS include_cols FROM sys.tables as t inner join sys.indexes as t1 on (t1.index_id > 0 and t1.is_hypothetical = 0) and (t1.object_id=t.object_id) WHERE t1.type in(1,2) ), indexInfo2 AS ( SELECT * ,(CASE WHEN is_primary_key = 1 THEN 'alter table '+[schema_name]+'.'+[table_name]+' add constraint '+[index_name]+' primary key '+(CASE WHEN [type]=1 THEN 'clustered' ELSE 'nonclustered' END)+'('+index_cols+')' WHEN is_unique = 1 AND is_unique_constraint = 1 THEN 'alter table '+[schema_name]+'.'+[table_name]+' add constraint '+[index_name]+' unique '+(CASE WHEN [type]=1 THEN 'clustered' ELSE 'nonclustered' END)+'('+index_cols+')' WHEN is_unique = 1 AND (is_primary_key = 0 OR is_unique_constraint = 0) THEN 'create unique '+(CASE WHEN [type]=1 THEN 'clustered' ELSE 'nonclustered' END)+' index '+[index_name]+' on '+[schema_name]+'.'+[table_name]+'('+index_cols+')' ELSE 'create '+(CASE WHEN [type]=1 THEN 'clustered' ELSE 'nonclustered' END)+' index '+[index_name]+' on '+[schema_name]+'.'+[table_name]+'('+index_cols+') ' END) script FROM indexInfo ) SELECT [schema_name],[table_name],[index_name],script +(CASE WHEN include_cols IS NOT NULL THEN ' include('+include_cols+')' ELSE '' END) +(CASE WHEN has_filter = 1THEN ' where '+filter_definition ELSE '' END) FROM indexInfo2 ORDER BY [schema_name],[table_name],[type],[index_name],is_primary_key DESC,is_unique_constraint DESC,is_unique DESC GO
批量導出存儲過程、函數和視圖:
use ReportServer$SQLSERVER select a.name,a.[type],b.[definition] from sys.all_objects a,sys.sql_modules b where a.is_ms_shipped=0 and a.object_id = b.object_id and a.[type] in ('P','V','AF') order by a.[name] asc
前一段時間,有個需求,就是對部分表進行了分庫,所以,原庫里面的存儲過程、視圖和函數里的表名等信息也要跟着更新,剛開始嘗試手動檢查了幾個存儲過程,可發現存儲過程太多,檢查起來效率很低,還容易出錯,況且還有視圖和函數,所以就想到了 Sql Server 內置的目錄視圖,找了一下,果然找到了解決辦法:
use ReportServer$SQLSERVER select a.name,a.[type],b.[definition] from sys.all_objects a,sys.sql_modules b where a.is_ms_shipped=0 and a.object_id = b.object_id and a.[type] in ('P','V','AF') order by a.[name] asc
從上面的SQL語句可以看出,主要用到了兩個 sys.all_objects 和 sys.sql_modules 兩個系統存儲過程,其中 sys.all_objects 是 sql server 2012 版本中的系統視圖,在 更早期的 sql server 版本中,應該用 sys.objects,同時,sys.objects 在 2012 中也是可以用的,只不過考慮到后續兼容性,在新版本中,用新的 name 還是比較好。
Sys.All_Objects(sys.objects
)
該視圖 很出名,是經常使用到的,主要是
:
顯示所有架構范圍內的用戶定義對象和系統對象的 UNION
主要字段:
1. Name:對象名
2. Object_id:對象標識號,在數據中是唯一的
3. Principal_id :架構所有者ID
4. Parent_object_id:此對象所屬對象的ID,0 = 不是子對象
5. Type:對象類型,常用的類型有, AF = 聚合函數
P = SQL 存儲過程
V = 視圖
TT = 表類型
U = 表(用戶定義類型)
6. Type_desc:對象類型的說明
7. Create_date / Modify_date :創建日期 / 修改日期
8. is_ms_shipped:是否為 內部 SQL Server 組建所創建的對象,常用來判斷 是否是 系統內置或用戶自定義 的對象
Sys.Sql_Modules
MSDN :
對每個 SQL 語言定義的模塊對象都返回一行。 類型為 P、RF、V、TR、FN、IF、TF 和 R 的對象均有關聯的 SQL 模塊。
該視圖 不是太常用,但是要返回 某些對象的 創建信息,如一個表格的 架構、字段等信息,就需要用這視圖了,稍后會在介紹字段后再介紹這個視圖。
注意,該視圖的作用域是當前 DB,並不是當前Server,所以使用前,一定注意前面加 use DBName 的信息
主要字段:
1.
Object_id:對象標識號,在數據中是唯一的
2.
Definition:用於定義此模塊的 SQL 文本
其實只要大家親自操作下,就很容易明白,
Definition 中的內容,就和下圖中的操作是一樣的:
本文首發博客園,原文地址:Sql Server 查看所有存儲過程或視圖的位置及內容 - http://www.cnblogs.com/xunziji/archive/2012/08/22/2650822.html
相關文章:
利用 sys.dm_exec_query_stats 查找並優化SQL語句
利用 sys.sysprocesses 檢查 Sql Server的阻塞和死鎖
Sql Server 系統表分析 (2) - 作業(job) 表
Sql Server 系統存儲過程分析 1 - 目錄存儲過程