SQL Server 資料庫部分常用語句小結
1. 查詢某儲存過程的訪問情況
SELECT TOP 1000 db_name(d.database_id) as DBName, s.name as 儲存名字, s.type_desc as 儲存型別, d.cached_time as SP新增到快取的時間, d.last_execution_time AS 上次執行SP的時間, d.last_elapsed_time as [上次執行SP所用的時間(微秒)], d.total_elapsed_time [完成此SP的執行所用的總時間(微秒)], d.total_elapsed_time/d.execution_count AS [平均執行時間(微秒)], d.execution_count as 自上次編譯以來所執行的次數 FROM sys.procedures s INNER JOIN sys.dm_exec_procedure_stats d ON s.object_id = d.object_id where s.name='儲存過程的名稱
2. 查詢包含關鍵字(字串)的儲存過程 SP
Select distinct o.* from sysobjects o, syscomments s where o.xtype='P' and s.id=o.id and text like '%關鍵字%' order by o.name
3. 查詢包含關鍵字(字串)的Job
Select * from msdb.dbo.sysjobs where job_id in(Select job_id from msdb.dbo.sysjobsteps where command like '%XXXXXX%' )
4. 查詢哪些SQL語句佔用CPU的頻率高
SELECT TOP 10 [cpu_time],[session_id],[request_id],[start_time] AS '開始時間',[status] AS '狀態',[command] AS '命令',dest.[text] AS 'sql語句', DB_NAME([database_id]) AS '資料庫名',[blocking_session_id] AS '正在阻塞其他會話的會話ID',[wait_type] AS '等待資源型別',[wait_time] AS '等待時間',[wait_resource] AS '等待的資源',
5. DB的單使用者模式更新成多使用者模式alter database 資料庫名字 set multi_user;
6.查詢某表的列的詳情select * from syscolumns where id=object_id('要統計的表') ---請替換
7.查詢當前SQL Server日誌資訊
Exec xp_readerrorlog 0
其實 xp_readerrorlog 一共有7個引數
1. 存檔編號
2. 日誌型別(1為SQL Server日誌,2為SQL Agent日誌)
3. 查詢包含的字串
4. 查詢包含的字串
5. LogDate開始時間
6. 結果排序,按LogDate排序(可以為降序"Desc" Or 升序"Asc")
7. 結果排序,按LogDate排序(可以為降序"Desc" Or 升序"Asc")
8.查詢Job 明細
SELECT b.[name] [JobName] ,b.enabled [Enabled] ,a.step_id [StepID] ,b.description [JobDescription] ,a.step_name [StepName] ,a.command [Script] FROM msdb.dbo.sysjobsteps a INNER JOIN msdb.dbo.sysjobs b ON a.job_id=b.job_id