?
Solved

sp_who2 ORDER BY

Posted on 2006-03-30
3
Medium Priority
?
8,836 Views
Last Modified: 2012-08-13
I need to find a way to run sp_who2, but then to have it sort by a column of my choosing.
0
Comment
Question by:SweatCoder
3 Comments
 
LVL 10

Assisted Solution

by:pai_prasad
pai_prasad earned 600 total points
ID: 16338670
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 600 total points
ID: 16338777
CREATE TABLE #tb1_sysprocesses (
  spid INT
 ,status sysname
 ,sid SYSNAME
 ,hostname sysname
 ,program_name sysname
 ,cmd varchar(300)
 ,cpu sysname
 ,physical_io sysname
 ,blocked sysname
 ,dbid sysname
 ,loginname sysname
 ,last_batch_char sysname )

insert INTO    #tb1_sysprocesses
exec sp_who2


select * from #tb1_sysprocesses
order by 2  -- you can choose your column name here


or  you can write another sp here

ALTER procedure sp_who3
( @orderby int )
AS

CREATE TABLE #tb1_sysprocesses (
  spid INT
 ,status sysname
 ,sid SYSNAME
 ,hostname sysname
 ,program_name sysname
 ,cmd varchar(300)
 ,cpu sysname
 ,physical_io sysname
 ,blocked sysname
 ,dbid sysname
 ,loginname sysname
 ,last_batch_char sysname )

insert INTO    #tb1_sysprocesses
exec sp_who2

declare @sql nvarchar(300)
set @sql = 'select * from #tb1_sysprocesses
    order by '+ cast(@orderby as char(1))  
exec sp_executesql @sql

drop table #tb1_sysprocesses

GO
exec sp_who3 2
0
 
LVL 11

Author Comment

by:SweatCoder
ID: 16343484
Perfect, thanks to both of you.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question