Determine Who did What from Transaction logs - sql-server

We received full backup and transaction logs files from a third party, which is used to restore our local SQL Server instance.
We are trying to know who did what from these files. I searched online and my query looks like:
The problem is Transaction SID column is always empty. Is there any way to fix this?
SELECT so.*, Operation, [Transaction SID],SUSER_NAME([Transaction SID]) AS [LoginName]
FROM fn_dump_dblog(NULL,NULL,'DISK',1
,'C:\yardi_tran\extract\201911130900.bak0'
,'C:\yardi_tran\extract\201911130900.bak1'
,'C:\yardi_tran\extract\201911130900.bak2'
,'C:\yardi_tran\extract\201911130900.bak3'
,'C:\yardi_tran\extract\201911130900.bak4'
,'C:\yardi_tran\extract\201911130900.bak5'
,'C:\yardi_tran\extract\201911130900.bak6'
,'C:\yardi_tran\extract\201911130900.bak7'
,'C:\yardi_tran\extract\201911130900.bak8'
,'C:\yardi_tran\extract\201911130900.bak9'
,'C:\yardi_tran\extract\201911130900.bak10'
,'C:\yardi_tran\extract\201911130900.bak11'
,'C:\yardi_tran\extract\201911130900.bak12'
,'C:\yardi_tran\extract\201911130900.bak13'
,'C:\yardi_tran\extract\201911130900.bak14'
,'C:\yardi_tran\extract\201911130900.bak15'
,'C:\yardi_tran\extract\201911130900.bak16'
,NULL,NULL,NULL
,NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL,NULL,NULL
,NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL,NULL,NULL
,NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL,NULL,NULL
,NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL,NULL,NULL
,NULL,NULL,NULL,NULL) blog
INNER JOIN
sys.partitions sp ON blog.PartitionId=sp.partition_id
INNER JOIN
sys.objects so ON so.object_id = sp.object_id
WHERE blog.Operation ='LOP_DELETE_ROWS'
AND so.type_desc='USER_TABLE'

Related

Query System Views From Linked 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

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

How do you find the permissions for a SQL Server table_type?

Hopefully a simple question here:
How do I get the permissions for an SQL Server table_type?
I know how to grant them, ie like:
GRANT [permission] ON TYPE::[schema_name].[type_name] TO [user]
As per http://technet.microsoft.com/en-us/library/ms174346.aspx.
What I want to know however, is how to find out what permissions users already have on a particular table_type. I've tried looking through tables like INFORMATION_SCHEMA.TABLE_PRIVILEGES, sys.database_permissions and sys.syspermissions, but I haven't had any luck.
My guess is I'm either missing something obvious or that the privileges for table_types are stored elsewhere.
You'll want to use the user_type_id from sys.table_types to join to sys.database_permissions:
SELECT prmssn.*
FROM sys.table_types AS tt INNER JOIN sys.database_permissions AS prmssn ON prmssn.major_id=tt.user_type_id
WHERE tt.name='<Table-Type Name>'
AND SCHEMA_NAME(tt.schema_id)='<SchemaName>')
Try this:
SELECT *
FROM sys.database_permissions a
JOIN sys.database_principals b on a.grantee_principal_id = b.principal_id
where major_id=object_id('dbo.TableNameHere')

Database backup in SQL Server 2008

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'

How do I find out what tables have data in a file in SQL Server?

I want to drop a now supposedly redundant file in SQL Server (2005), but when I try to drop it I am told that the file is not empty. Does anyone know of a way to find out what data is still in this file so I can make whatever changes I need to allow me to drop it?
Assuming you're moved the table etc, you'll probably need to run:
DBCC SHRINKFILE (MyLogicalFile, EMPTYFILE) --EMPTYFILE is the important bit!!
See DBCC SHRINKFILE
To check (this is a cut'n'paste of a usage script I use):
SELECT
ds.[name] AS LogicalFileName,
OBJECT_NAME(p.object_id) AS Thing,
SUM(au.total_pages) / 128.0 AS UsedMB,
df.size / 128 AS FileSizeMB,
100.0 * SUM(au.total_pages) / df.size AS PercentUsed
FROM
sys.database_files df
JOIN
sys.data_spaces ds ON df.data_space_id = ds.data_space_id
JOIN
sys.allocation_units au ON ds.data_space_id = au.data_space_id
JOIN
sys.partitions p ON au.container_id = p.hobt_id
WHERE
OBJECTPROPERTYEX(p.object_id, 'IsMSShipped') = 0
GROUP BY
ds.[name], OBJECT_NAME(p.object_id), df.size
ORDER BY
ds.[name]

Resources