views:

37

answers:

2

Say if using sp_helplognis, want to view result set with filter UserName=db_owner. Is there any way besides output the original result set to a temp table and then query on that temp table? Thanks.

A: 

You can divert the resultset of a sproc into a new table, e.g.

create table #results (...)
insert into #results execute myproc
select * from #results where ...

It's a bit fiddly because you have to reverse-engineer the resultset data types pretty exactly to avoid casting errors.

Christian Hayter
What if the SP returns 2 result sets?
Stan
So what would people do if they need refine the SP output result set?
Stan
I have just checked MSDN, and it appears that all resultsets are loaded into the one table. That's no use if the resultsets differ in column definitions I'm afraid.
Christian Hayter
+3  A: 

Don't use sp_helplogins: use sys.server_principals

SELECT * FROM  sys.server_principals WHERE name = 'bob'

If you want the 2nd resultset of sp_helplogins, then you'd have to use c# or something because loading a temp table will only trap the 1st resultset.

The equivalent of sys.server_principals is sys.database_principals too

FYI: db_owner is in every database already. What are you really trying to do...?

Edit, after comment, something like:

EXEC sp_MSForEachDb '
USE ?
IF DATABASE_PRINCIPAL_ID (''myloginname'') IS NOT NULL
  EXEC sp_addrolemember ''db_datareader'', ''myloginname''
'
gbn
I want to find what databases are belongs to a specific user and run below script: <code>USE DbNameGOEXEC sp_addrolemember db_datareader, myloginnameGO</code>
Stan