Database backup in SQL Server 2008 - sql-server

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'

Related

Determine Who did What from Transaction logs

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'

SQL Server Database mail does not work

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.

Check when SQL backup failes via Powershell

Is it possible to capture info when the sql backup was run? And if the backup failed?
I use MSSQL and Powershell
This query will give you information about backups including when it was last performed. It will not show if the backup failed, because failed backups are not listed as backups. You should find those in the job overview or history. If you use powershell, there will be no logging for a failed backup. (as far as I know)
This query is also used in Sequenchel(.com) for monitoring backups
SELECT isnull(y.server_name,CAST(SERVERPROPERTY('ServerName') AS nvarchar(255))) as ServerName,
x.name as databaseNameM, y.database_name as DatabaseNameB,
CAST(DatabasePropertyEx(x.name,'Recovery') AS nvarchar(15)) as RecoveryModel ,
case y.type
when 'D' then 'FULL BCK'
when 'I' then 'DIFF BCK'
when 'L' then 'TRAN BCK'
when 'F' then 'FILE BCK'
else 'UNKNOWN!'
end as BackupType,
y.backup_finish_date as LastBackup,
y.Bck_Location as Location
FROM master.dbo.sysdatabases x
FULL OUTER JOIN
(SELECT max_bck.server_name,max_bck.database_name,max_bck.backup_finish_date,max_bck.type,
left(fam.physical_device_name,len(fam.physical_device_name)-patindex('%\%',reverse(fam.physical_device_name))+1) AS Bck_Location
FROM (select bak.server_name,bak.database_name,max(bak.backup_finish_date) as backup_finish_date,bak.type
from msdb.dbo.backupset bak
group by bak.server_name,bak.database_name,bak.type) max_bck
INNER JOIN msdb.dbo.backupset bak
ON max_bck.backup_finish_date = bak.backup_finish_date
and max_bck.type = bak.type
and max_bck.database_name = bak.database_name
INNER JOIN msdb.dbo.backupmediafamily fam
ON bak.media_set_id = fam.media_set_id
) y
ON x.name = y.database_name
WHERE isnull(x.name,y.database_name) !='tempdb'
AND (y.server_name IS NULL OR y.server_name = CAST(SERVERPROPERTY('ServerName') AS nvarchar(255)))
AND CAST(DatabasePropertyEx(x.name,'Status') AS nvarchar(15)) = 'ONLINE'
ORDER BY ServerName,isnull(x.name,y.database_name),type

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