Create Temp Table for SP_WHO
2011-10-22 04:57
316 查看
Those days, I worked on some SQL Server issue, and i fond that there have very powerful and useful command to get spid and database relation,
that is SP_WHO;
but this command is just a system stored procedures, here is the link from MSDN for SQL Server 2008 R2;so we can not select or filter and order result of this procedure;
but we can create a temp table for result, and execute some query on the table; so first we need to create a temp table with the same columns as SP_WHO;(Here is SQL Server 2008 R2, you can check about the msdn for the different version of SQL Server, because
the column maybe have some differences between different SQL Server version);
In case the table name is exist in the database; we need check about it if it's exist already;
and then put the record into the temp table;
and then u can make a filter or order to this table; like select or filter and order; normally we need the spid and the some special dbname record, so...
that is SP_WHO;
SP_WHO
but this command is just a system stored procedures, here is the link from MSDN for SQL Server 2008 R2;so we can not select or filter and order result of this procedure;
but we can create a temp table for result, and execute some query on the table; so first we need to create a temp table with the same columns as SP_WHO;(Here is SQL Server 2008 R2, you can check about the msdn for the different version of SQL Server, because
the column maybe have some differences between different SQL Server version);
In case the table name is exist in the database; we need check about it if it's exist already;
IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects WHERE id = OBJECT_ID(N'tempdb..#temp') AND xtype='U') DROP TABLE [#temp] GO
CREATE TABLE #temp ( spid INT, ecid INT, status VARCHAR(50), loginame VARCHAR(255), hostname VARCHAR(255), blk VARCHAR(255), dbname VARCHAR(255), cmd VARCHAR(255), request_id INT )
and then put the record into the temp table;
INSERT INTO #temp EXEC SP_WHO
and then u can make a filter or order to this table; like select or filter and order; normally we need the spid and the some special dbname record, so...
SELECT * FROM #temp WHERE dbname ='yourdbname'
相关文章推荐
- sqlserver sp_msforeachtable用法
- sp_MSforeachtable使用方法
- [翻译]SQL Server 未公开的两个存储过程sp_MSforeachtable 和 sp_MSforeachdb
- ora-01658 unable to create initial extent for segment in tablespace
- 一个值得研究的系统存储过程 sp_MSforeachtable
- 【推荐】(SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解
- 详解SQL Server的两个存储过程:sp_MSforeachtable/sp_MSforeachdb
- sp_MSforeachtable的作用
- sp_MSforeachtable 与 sp_MSforeachdb
- 微软不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解-统计数据库-记录数-容量-存储过程脚本等
- ORA-01658:unable to create INITIAL extent for segmnet in tablespace XXX
- sp_MSforeachtable/sp_MSforeachdb (转)
- sp_MSforeachtable使用方法[转]
- 两个有用的,ms未公开的存储过程sp_MSforeachDB,sp_MSforeachtable
- 详解SQL Server的两个存储过程:sp_MSforeachtable/sp_MSforeachdb
- sp_MSforeachtable 内置存储过程的运用
- SQL存储过程 之 sp_MSforeachtable和sp_MSforeachDB
- 【推荐】(SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解
- 树莓派使用Tab键后提示bash: cannot create temp file for here-document: No space left on device
- sp_MSforeachtable使用方法