HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

Store output of sp_who2 in a table

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
tableoutputstoresp_who2

Problem

I am looking for something like this, where I could store the output of sp_who2 in a table, without having to first create the table:

create table test as exec sp_who2

Solution

USE yourDb;

CREATE TABLE temp_sp_who2
    (
      SPID INT,
      Status VARCHAR(1000) NULL,
      Login SYSNAME NULL,
      HostName SYSNAME NULL,
      BlkBy SYSNAME NULL,
      DBName SYSNAME NULL,
      Command VARCHAR(1000) NULL,
      CPUTime INT NULL,
      DiskIO BIGINT NULL, -- int
      LastBatch VARCHAR(1000) NULL,
      ProgramName VARCHAR(1000) NULL,
      SPID2 INT
      , RequestId INT NULL --comment out for SQL 2000 databases
 
    )
 
 
INSERT  INTO temp_sp_who2
EXEC sp_who2
 
SELECT  *
FROM    temp_sp_who2
WHERE   DBName = 'yourDb'


Link

Just tested it on SQL server 2012

Code Snippets

USE yourDb;

CREATE TABLE temp_sp_who2
    (
      SPID INT,
      Status VARCHAR(1000) NULL,
      Login SYSNAME NULL,
      HostName SYSNAME NULL,
      BlkBy SYSNAME NULL,
      DBName SYSNAME NULL,
      Command VARCHAR(1000) NULL,
      CPUTime INT NULL,
      DiskIO BIGINT NULL, -- int
      LastBatch VARCHAR(1000) NULL,
      ProgramName VARCHAR(1000) NULL,
      SPID2 INT
      , RequestId INT NULL --comment out for SQL 2000 databases
 
    )
 
 
INSERT  INTO temp_sp_who2
EXEC sp_who2
 
SELECT  *
FROM    temp_sp_who2
WHERE   DBName = 'yourDb'

Context

StackExchange Database Administrators Q#59472, answer score: 26

Revisions (0)

No revisions yet.