SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Domain users not in sys.server_principles


Domain users not in sys.server_principles

Author
Message
ken.trock
ken.trock
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1255 Visits: 1730
Hello. I'm trying to find out who owns SQL Agent jobs on one of our servers where there's a lot of jobs.


SELECT sj.name AS 'job', sp.NAME AS 'owner', spr.NAME AS 'proxy', sj.*
FROM msdb..sysjobs sj
LEFT JOIN sys.server_principals sp ON sj.owner_sid=sp.sid
LEFT JOIN msdb..sysjobsteps sjt ON sj.job_id = sjt.job_id
LEFT JOIN msdb..sysproxies spr ON spr.proxy_id=sjt.proxy_id
WHERE sj.enabled=1



A lot of these come back with NULL as the owner; most likely because that user has their access thru a domain group rather than having their individual login defined on the server. If you look at 1 of the jobs with no owner in the GUI it will display. And there is a valid looking owner_sid in sysjobs for these entries.

So how is the GUI getting it right? Ermm Is it some kind of Active Directory query or maybe sys.server_principles isn't the right table to query.

Thanks,
Ken
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: General Forum Members
Points: 36770 Visits: 14411
You cannot rely on sys.server_principals to resolve the names of job owners that are entering the instance via a Windows Group. Use SUSER_SNAME() instead:

SELECT  sj.name AS 'job',
SUSER_SNAME(owner_sid) AS 'owner',
spr.NAME AS 'proxy',
sj.*
FROM msdb.dbo.sysjobs sj
LEFT JOIN msdb.dbo.sysjobsteps sjt ON sj.job_id = sjt.job_id
LEFT JOIN msdb.dbo.sysproxies spr ON spr.proxy_id = sjt.proxy_id
WHERE sj.enabled = 1



If the column is still NULL using SUSER_SNAME() then the SID that owns the job is no longer linked to a valid Windows User, i.e. they were deleted from a machine or domain.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
ken.trock
ken.trock
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1255 Visits: 1730
Thanks a lot! SUSER_SNAME() is doing it.

Ken
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search