- 查询所有用户表:select [id], [name] from [sysobjects] where [type] = 'u'
- 查询所有库:select [name] from [sysdatabases] order by [name]
- 查询表的所有字段:select * from sys.columns where object_id='数据表ID' --(可以在sysobjects找到)
- 查询数据库中所有的存储过程和视图:
select a.name,a.[type],b.[definition],b.* 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
- 查询阻塞语句:
SELECT TOP 10
[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 '等待的资源',
[reads] AS '物理读次数',
[writes] AS '写次数',
[logical_reads] AS '逻辑读次数',
[row_count] AS '返回结果行数'
FROM sys.[dm_exec_requests] AS der
CROSS APPLY
sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id]>50 AND DB_NAME(der.[database_id])='数据库名称'
ORDER BY [cpu_time] DESC