I had database e-mail setup and it was working for the past three years or so. Now all of a sudden it is not working. When I run the following query I see no users
SELECT l.name LoginName, u.name UserName, r.name RoleName
FROM master.sys.server_principals l
JOIN msdb.sys.database_principals u
ON u.sid = l.sid
LEFT JOIN msdb.sys.database_role_members rm
ON rm.member_principal_id = u.principal_id
LEFT JOIN msdb.sys.database_principals r
ON r.principal_id = rm.role_principal_id
WHERE l.name = SUSER_NAME()
AND r.name IN ('db_owner', 'DatabaseMailUser')
It seems like someone went in changed all the permissions of all the users. What should I do to get my e-mails working again? I have SQL Server 2008 R2.
Related
How to get active sessions details with database and object details in SQL Server?
The sys.dm_exec_sessions view reports all authenticated sessions within SQL Server.
You can use the following query;
SELECT * FROM sys.dm_exec_sessions
For more detailed query you can use this;
SELECT s.session_id, s.login_time, s.host_name, s.program_name,
s.login_name, s.nt_user_name, s.is_user_process,
s.database_id, DB_NAME(s.database_id) AS [database],
s.status,
s.reads, s.writes, s.logical_reads, s.row_count,
c.session_id, c.net_transport, c.protocol_type,
c.client_net_address, c.client_tcp_port,
c.num_writes AS DataPacketWrites
FROM sys.dm_exec_sessions s
INNER JOIN sys.dm_exec_connections c
ON s.session_id = c.session_id
INNER JOIN sys.dm_exec_requests r
ON s.session_id = r.session_id
I'm tasked with creating a report that will pull down the permissions from different servers and display them.
I'm having an issue with the query not picking up all of the rows in from the system view, from another server.
When I run the below query on serverA, it gives me 251 results.
SELECT COUNT(*) FROM ServerA.employee.sys.objects
When I run the same code from ServerB, I get 153 results.
I compared the two and it looks like the linked server isn't pulling type_desc of SQL_SCALAR_FUNCTION, SQL_STORED_PROCEDURE, and SYSTEM_TABLE.
Does anyone know way I can get a list of database object permissions running from a different server in SQL server?
If you are using SSRS for the report, you can setup a dynamic data source to run the same report for multiple servers/databases. I'd use a static data source at first to setup the datasets, otherwise the columns will not get created automatically.
="Data Source=" & Parameters!ServerName.Value & ";Initial Catalog=" & Parameters!DatabaseName.Value
Here is the listing of permissions on schema objects within a database.
SELECT
pr.principal_id
, pr.name
, pr.type_desc
, pr.authentication_type_desc
, pe.state_desc
, pe.permission_name
, ObjectName = s.name + '.' + o.name
FROM
sys.database_principals AS pr
INNER JOIN sys.database_permissions AS pe ON pe.grantee_principal_id = pr.principal_id
INNER JOIN sys.objects AS o ON pe.major_id = o.object_id
INNER JOIN sys.schemas AS s ON o.schema_id = s.schema_id;
Microsoft Reference
We license our software by number of workstations.
I have a query that I have used for years to get an accurate count of the workstations logged in to my SQL Server database. For simplicity, all users use the same login name/password. This is built in to the script that attaches to the DB. They have access only to that DB with the exception of
USE [Master] GRANT VIEW SERVER STATE to MyUser
The query that has been working is below:
SELECT COUNT(Users) AS UserCount FROM (SELECT COUNT(Master.dbo.sysprocesses.hostname) AS Users FROM Master.dbo.sysprocesses LEFT OUTER JOIN Master.dbo.sysdatabases ON Master.dbo.sysdatabases.dbid = Master.dbo.sysprocesses.dbid WHERE (Master.dbo.sysdatabases.name = 'MyDatabase') GROUP BY Master.dbo.sysprocesses.net_address) AS UserCount_1
Basically this relies on the mac address (Master.dbo.sysprocesses.net_address), since both Workstation names and ip addresses can be duplicated.
Recently this has stopped working at a number of customers. Suddenly individual workstations are showing multiple net addresses for the same workstation causing a substantial overcount of users. This may be related to SQL Server 2012 - not sure.
What I need is a very reliable way to get a count of workstations logged in to my database.
If anyone can tell me why I am suddenly getting multiple net_addresses for each workstation and how to prevent that that would be one possible solution.
Otherwise if anyone can give me a rock solid way to get a workstation count other than the above that would be great. Our largest customer is 50 users by the way.
HERE IS AN EXAMPLE:
SELECT Master.dbo.sysprocesses.hostname AS Users, Master.dbo.sysprocesses.net_address FROM Master.dbo.sysprocesses
LEFT OUTER JOIN Master.dbo.sysdatabases ON Master.dbo.sysdatabases.dbid = Master.dbo.sysprocesses.dbid
WHERE Master.dbo.sysdatabases.name = 'mydb' GROUP BY Master.dbo.sysprocesses.hostname, Master.dbo.sysprocesses.net_address
RETURNS:
DAVID-PC 001CC490239E
FLOOR1 001CC41D8012
FLOOR2 CB8FEE6C5856
FLOOR3 A50B18FF1516
KER-PC7 6C626DEA68CC
LIZ-PC A4E553460E35
LIZ-PC EFE3F0E20260
LIZ-PC FD32F7B30360
PAP 9D35A704C29C
PAP CFB724BA1183
PAP D1A58A8878E6
PAP E9B116CA34B8
PAP F38B335A7AE6
Thanks in advance for any help.
You can get an accurate count of the users logged in to SQL Server database by the following query:
SELECT
DB_NAME(dbid) as DB
COUNT(dbid) as Numb
loginame as LoginNa
FROM
sys.sysprocesses
WHERE
dbid > 0
GROUP BY
dbid, loginame
Also you can get full details of connected users by this:
sp_who2 'Active'
Here is a way that will give you the number of IPs or MAC addresses connected to a given database. I'd go with IP as you might have multiple NICs/MACs in a given high end workstation.
SELECT COUNT(DISTINCT c.client_net_address) as DistinctIPCount,
COUNT(DISTINCT p.net_address) as DistinctMACCount
FROM sys.dm_exec_connections c INNER JOIN sys.dm_exec_sessions s ON c.session_id = s.session_id
INNER JOIN sysprocesses p ON s.session_id = p.spid
WHERE s.is_user_process = 1
AND p.dbid = DB_ID('yourdbnamehere')
...using SQL Server 2008 R2. I know how to find all objects a user has rights to, but how do I find all accounts that have execute rights on a particular object
Below query observed from How Can we find user have Execute Right on SP will help you get the required information (Not tested .. so minor tweak may be required).
SELECT
s.name AS SchemaName,
o.name AS ObjectName,
dp.name AS PrincipalName,
dperm.type AS PermissionType,
dperm.permission_name AS PermissionName,
dperm.state AS PermissionState,
dperm.state_desc AS PermissionStateDescription
FROM sys.objects o
INNER JOIN sys.schemas s on o.schema_id = s.schema_id
INNER JOIN sys.database_permissions dperm ON o.object_id = dperm.major_id
INNER JOIN sys.database_principals dp
ON dperm.grantee_principal_id = dp.principal_id
WHERE
dperm.class = 1 --object or column
AND
dperm.type = 'EX'
AND
dp.name = 'Specific_username'
AND
o.name = 'specific_object_name'
You did not specify whether you want it through T-SQL or Management Studio. For T-SQL you already have answer, for Management Studio just right-click the object (e.g. table, stored procedure), click Properties, and then select Permissions tab. Don't miss the blue links "View schema permissions", "View database permissions", "View server permissions".
How can i check with SQL query whether Database backup JOB is created or not ?
Thank You
This query will return all jobs that have the command BACKUP DATABASE in them:
SELECT SV.name,
SV.description,
S.step_id
FROM msdb.dbo.sysjobs_view SV
INNER JOIN msdb.dbo.sysjobsteps S ON SV.job_id = S.job_id
WHERE s.command LIKE '%BACKUP DATABASE%'
If you setup the job categories properly to have a category called 'Database Backup' the following would also work:
SELECT SV.name,
SV.description
FROM msdb.dbo.sysjobs_view SV
INNER JOIN msdb.dbo.syscategories SC ON SV.category_id = SC.category_id
WHERE SC.Name = 'Database Backup'