Query System Views From Linked Server - SQL Server - sql-server

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

Related

Stop SQL Tracing

my MSSQL DB creates every second two SQL Server Profil - tace data file:
Screenshot
How can I stop that, because we don't want to tracing something and it files my memory?
My SELECT * FROM sys.traces is empty.
If I execute SELECT * FROM sys.server_event_sessions, the result looks like the following Screenshot:
Additionally, I execute the following command:
SELECT s.name, CAST(st.target_data AS XML).value('(/EventFileTarget/File/#name)[1]', 'NVARCHAR(100)')
FROM sys.dm_xe_sessions s
INNER JOIN sys.dm_xe_session_targets st ON s.address = st.event_session_address
WHERE target_name = 'event_file
The result looks like this:
Next, I open the system_health-file and get this displayed:
The hkenginexesession-file is empty.
Do you have enough permission to see trace list?
The visibility of the metadata in catalog views is limited to securable that a user either owns or on which the user has been granted some permission. For more information, see Metadata Visibility Configuration.
If the list is still empty with sysadmin premission then check if these files are created by Extended events:
USING SSMS:
Server > Management > Session
USING TSQL
SELECT s.name, CAST(st.target_data AS X`enter code here`ML).value('(/EventFileTarget/File/#name)[1]', 'NVARCHAR(100)')
FROM sys.dm_xe_sessions s
INNER JOIN sys.dm_xe_session_targets st ON s.address = st.event_session_address
WHERE target_name = 'event_file'
The above query return session name and filename of outputs.

using sys.columns from a different databases and servers

Solution specified here, works as follows (I run from server1)
select * from server1.blahdbname.sys.columns c
where c.[object_id] = OBJECT_ID('blahdbname.dbo.blahtablename')
It is fine. Works as intended. But below query doesn't work (changed servername) (returns empty set)
select * from server2.blahdbname.sys.columns c
where c.[object_id] = OBJECT_ID('blahdbname.dbo.blahtablename')
What is the universal way if I want to query different servers too? Above queries generated dynamically, so I want them to work on any server and db
NOTE: blahdbname and blahtablename both exist in server1 and server2. server1 and server2 are linked
This is because you are using the function OBJECT_ID. This runs against the current database, not the remote database.
Instead you should use the system views on the remote server to make this happen.
select c.*
from server1.blahdbname.sys.columns c
join server1.blahdbname.sys.tables t on c.object_id = c.object_id
where t.name = 'blahtablename'

microsoft sql select query from multiple tables

This query is working in mysql but is not working in microsoft sql server management studio 2008, can someone help me out?
SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid
FROM cms_question_report QR,
cms_clients C,
cms_questioncategory QC ,
cms_reporthistory RH
WHERE C.id=QR.cid
AND QR.rhid=RH.id
AND QR.catid='3'
AND QR.catid=QC.id
I am getting the error: Invalid object name cms_question_report
SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid
FROM cms_question_report QR
left join cms_clients C
on C.id=QR.cid
left join cms_questioncategory QC
on QR.catid=QC.id
and QR.catid='3'
left join cms_reporthistory RH
on QR.rhid=RH.id
I think this should do
specify Normally it happens when you have specific schema and you don't specify it for example:
Replace dbo. with your schema and/or type your database name
SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid
FROM databasename.dbo.cms_question_report QR,
databasename.dbo.cms_clients C,
databasename.dbo.cms_questioncategory QC ,
databasename.dbo.cms_reporthistory RH
WHERE C.id=QR.cid
AND QR.rhid=RH.id
AND QR.catid='3'
AND QR.catid=QC.id

How to find all users with execute rights on a stored procedure in SQL Server

...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".

Retrieve file list of an SQL server database which is offline

I have some offline databases on a SQL server. I would like to know which files on disc are related to these databases. Is it possible to retrieve the file list of offline databases without taking them online first?
This will give you a list of all physical file paths related to any offline databases, along with database name and file type:
SELECT
'DB_NAME' = db.name,
'FILE_NAME' = mf.name,
'FILE_TYPE' = mf.type_desc,
'FILE_PATH' = mf.physical_name
FROM
sys.databases db
INNER JOIN sys.master_files mf
ON db.database_id = mf.database_id
WHERE
db.state = 6 -- OFFLINE
Or simply
select * from sys.databases where state_desc='OFFLINE'
List all the available, but offline SQL server database files
The following statement will list all the files associated with offline SQL server databases
SELECT
m.physical_name + '\' + m.name AS [file_path]
FROM
sys.databases AS d
INNER JOIN sys.master_files AS m ON d.database_id = m.database_id
WHERE
d.state_desc = 'OFFLINE'
--AND m.type_desc = 'ROWS'
GROUP BY
m.physical_name + '\' + m.name
Note: Uncomment the line AND m.type_desc = 'ROWS' (delete the --) to filter the list further to include only database files. Otherwise, log files will also be listed.
The GROUP BY clause is there to prevent entries appearing more than once.

Resources