Sunday, February 19, 2012

add another column to sproc output

Say i have sproc that return rows with x columns.
Say I now want to add another column to it.
For eg: Say I am running sp_who2
But now Say i want to add a getdate column to it and i want to do something
like
Select getdate() + exec sp_who2There *might* be *better* ways to do this, but my general purpose sp_who2
script might help you with this specific requirement..
go
if OBJECT_ID('tempdb..#spwho') > 0 drop table #spwho
go
create table #spwho (
SPID int not null
, Status varchar (255) not null
, Login varchar (255) not null
, HostName varchar (255) not null
, BlkBy varchar(10) not null
, DBName varchar (255) null
, Command varchar (255) not null
, CPUTime int not null
, DiskIO int not null
, LastBatch varchar (255) not null
, ProgramName varchar (255) null
, SPID2 int not null
)
go
insert #spwho
exec sp_who2
go
select getdate(), *
from #spwho
--where SPID > 50 and login != SUSER_SNAME()
order by SPID --LastBatch desc
go
if OBJECT_ID('tempdb..#spwho') > 0 drop table #spwho
HTH
Regards,
Greg Linwood
SQL Server MVP
"Hassan" <hassanboy@.hotmail.com> wrote in message
news:eH74sHz6FHA.472@.TK2MSFTNGP15.phx.gbl...
> Say i have sproc that return rows with x columns.
> Say I now want to add another column to it.
> For eg: Say I am running sp_who2
> But now Say i want to add a getdate column to it and i want to do
> something like
> Select getdate() + exec sp_who2
>
>|||Hassan,
This technique might be more generally useful to you:
SELECT getDate() as CurrentTime, *
FROM OPENROWSET ('SQLOLEDB',
'Server=(local);Database=master;Trusted_
Connection=yes', 'SET FMTONLY OFF;
exec sp_who2');
You may have to change the connection string if you are not using Windows
authentication for some reason.
Cheers,
Chris
"Hassan" wrote:

> Say i have sproc that return rows with x columns.
> Say I now want to add another column to it.
> For eg: Say I am running sp_who2
> But now Say i want to add a getdate column to it and i want to do somethin
g
> like
> Select getdate() + exec sp_who2
>
>

No comments:

Post a Comment