SQL Server Agent Database Restore Job Failure - sql-server

An SSA database restore job fails sporadically with this error:
String data, right truncation [SQLSTATE 01004]
This job runs daily, and succeeds most of the time. When it fails it is on a Friday and it leaves the database in a restoring state. The code inside the job is the exact same code used for other database restore jobs where this is not a concern.
My solution each time this happens is to delete the database and manually restore it. After manually restoring, I run the job and it runs fine. It continues to run fine every day from that point on until it doesn't.
I checked the SQL logs and I do see this error:
"spid75,Unknown,BackupDiskFile::OpenMedia: Backup device '\the_path_to_the_backups' failed to open. Operating system error 5(Access is denied.).,,,,".
So, I checked to ensure the user has permission to access to the .BAK file at the folder level and the file level. It does have full access.
Any thoughts on how I can stop this job from failing randomly?
EDIT:
This is the code from that job.
USE DBA_Tools;
GO
EXECUTE
DBMaintenance.restoreBackupChain
#BackupFolder = '\\servername\sqlbackups2$\instancename\Database_Backups\DSOG',
#Database = 'DSOG';
USE [DSOG]
GO
EXEC sp_change_users_login 'Auto_Fix', 'reportingServicesUser'
GO
EXEC [DSOG].dbo.sp_changedbowner #loginame = N'sa', #map = false
GO
USE [master] ;
ALTER DATABASE [DSOG] SET RECOVERY SIMPLE ;
GO

Related

Database Keeps Going into Recovery Pending State

I have a SQL server database that has been running perfectly fine on my machine for about 6 months, a couple days ago out of nowhere it was inaccessible (Pending Recovery).
I did a bunch of Googling and have tried the following things to fix the issue but thus far restoring it from a previous backup is the only thing that seems to work.
I have tried (From SMS and SQLCMD):
ALTER DATABASE mydatabase SET EMERGENCY
ALTER DATABASE mydatabase set single_user
DBCC CHECKDB (mydatabase, REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS;
ALTER DATABASE mydatabase set multi_user
Step #3 errors out with: "cannot open mydatabase is already open and can only have one user at a time"
Second try:
EXEC sp_resetstatus 'mydatabase';
ALTER DATABASE mydatabase SET EMERGENCY
DBCC CHECKDB ('mydatabase')
ALTER DATABASE mydatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DBCC CHECKDB ('mydatabase', REPAIR_ALLOW_DATA_LOSS)
ALTER DATABASE mydatabase SET MULTI_USER
Step #5 errors out with the same error.
My question is what could be causing this in the first place and how can I fix it properly without having to do a restore twice a day.
Database is already open and can only have one user at a time, this is error number 924. The complete error message looks like this:
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
Msg 924, Level 14, State 1, Line 1 Database ‘db_name’ is already open and can only have one user at a time.
The level 14 belongs to security level errors like a permission denied. It means that it cannot be open because someone is using it.
Use the sp_who or sp_who2 stored procedures. You can also use the kill command to kill the processes that are active in the database.
I also found this thread useful: How to fix Recovery Pending State in SQL Server Database?
what could be causing this in the first place and how can I fix it properly without having to do a restore
The most likely cause is a a hardware or driver problem with your hard disk.
In my case, I had databases set up on my local machine but on an external drive mapped to my hard drive. I have the external drive connected to my docking station all the time but I had to disconnect the hard drive and after I connected it again - the databases that are restored on the external drive went into Recover Pending mode.
In my case what helped me was to set the database offline in Microsoft SQL Server Management Studio by right clicking on the database - Tasks - Take Offline. The status of the database changes to Offline. After that bring the database online again by right clicking on the database - Tasks - Bring online.
The database was successfully recovered without any issues. But if the cause is different these steps may not help.
Take the database offline
Bring the database back online

Database in SQL server in recovery mode

I have two databases (at the same server) in Microsoft SQL server. One of them can be successfully accessed remotely. However, the other not. It returns the following message in the error log:
Login failed for user 'adminUsr'. Reason: Failed to open the explicitly specified database 'alg_test.alg_test'. [CLIENT: ]
Error: 18456, Severity: 14, State: 38.
Then I go to Microsoft SQL server management and check the status of the database with:
SELECT databasepropertyex('alg_test.alg_test', 'STATUS')
and got this:
RECOVERING
It seems that the database is constantly recovering. How can I fix this? and finally geet access to the database remotely.
Check the SQL Server error log for related messages to see why the database is recovering. Common causes include:
The database was restored with the NORECOVERY option from full, differential, and log backups but RECOVERY was not specified on the last restore. The solution in this case is simply execute RESTORE <your database> WITH RECOVERY; to rollback uncommitted transactions and bring the database online.
The transaction log filled due to a large data modification operation and SQL Server is rolling the transactions(s) back to recover the database, which can take quite a bit of time. The error log will include recovery progress messages. It that's longer than you want to wait, it may be more expeditious to restore the database from backup(s). Be aware that if SQL Server is restarted during the database recovery process, recovery will restart from the beginning at service startup.
Well you could try this script
RESTORE DATABASE mydatabase WITH RECOVERY
SO this will finish the recovery process with no backup files. If you are getting an error message 'database in use'. I think you shoudl try to stop the service, delete the databse and then Restore it with 'Recovery'
Try this one :
Use [master]
GO
RESTORE DATABASE [DataBaseName] WITH RECOVERY

Can I both finish restoring a DB backup and query that DB in the same stored procedure?

Here's my setup. Once a day, a full backup of my DB is retrieved from the production server and restored onto a local SQL Server instance. Every 15 minutes aftwerwards, a SQL transaction log is retrieved from production and restored locally.
RESTORE DATABASE [DBNAME] from disk=#path with NORECOVERY, REPLACE)
RESTORE LOG [DBNAME] from disk=#path with NORECOVERY
In case of a failure of the production environment, I need to be able to use the local DB instead. This means "finishing up the restore" and changing some configuration values like this :
RESTORE DATABASE [DBNAME] with RECOVERY
UPDATE [DBNAME].dbo.[TABLE] SET [COL1] = 1
I have put this code in a stored procedure (in another DB on the same SQL Server instance). However, I am unable to execute it as the second line causes an error:
Database 'DBNAME' cannot be opened. It is in the middle of a restore.
I assume this is due to pre-validation by the SQL Server engine (since the DB is not available until the RESTORE query is executed), but I would like to know how to work around it as cleanly as possible. I found a workaround, which I posted as an answer below, but it's definetely not a great way to solve the problem.
Thanks for the help!
You should be able to work around this by placing the second statement in an EXEC:
RESTORE DATABASE [DBNAME] with RECOVERY
EXEC('UPDATE [DBNAME].dbo.[TABLE] SET [COL1] = 1')
The issue you're likely seeing is that SQL Server wants to compile the entire stored procedure before it starts executing. In order to compile the UPDATE, it needs to, at the very least, confirm the existence of the table and column(s) involved.
So, put it in an EXEC so that it's not compiled until that part of the procedure is reached.
The workaround I have found is simple:
Create a stored procedure that executes the query on the database.
Call this SP in the first stored procedure.
However, I'd like to avoid cluttering up my SQL Server databases with unnecessary stored procedures if at all possible!
If you know roughly long it takes to restore you could put a delay in it maybe?
restore database [DBName] with recovery;
Begin
waitfor delay '00:01'; --one minute delay
update [DBTable] set [Col1]= 1;
END;

SQL Server production server - all databases are in recovery pending state

All the databases in my SQL Server production server are in recovery pending state. I tried to execute different queries but they were of no use. Please help me as production work has been stopped at client side.
Tried to execute alter commands - but show error as following:
Msg 5120, Level 16, State 101, Line 1 Unable to open the physical file
"G:\Data\MSSQL\Database.mdf". Operating system error 3: "3(The system
cannot find the path specified.)". File activation failure. The
physical file name "G:\Data\MSSQL\Data\Database_log.ldf" may be
incorrect. Msg 945, Level 14, State 2, Line 1 Database 'Database'
cannot be opened due to inaccessible files or insufficient memory or
disk space. See the SQL Server errorlog for details
Msg 5069, Level 16, State 1, Line 1
Recovery pending means that for some reason SQL cannot run restart recovery on the database. Usually this is because the log is missing or corrupt.
Check to see if you can find the Database.mdf and Database_log.ldf files in the folder specified.
Check your system has not run out of disk space.
This could be caused by a hard drive failure. You may need to do a full restore of your last full back, any differentials and then restore the logs up until the log error occurred.
See similar issue here
My team encountered this error many times for my clients & I know, It is not easy to manage in the Production server. In your case Error 5120 –This error comes when the database is in Read Only Mode.
To fix this you can below code
USE [master]
GO
ALTER DATABASE [SQLAuthority] SET READ_WRITE WITH NO_WAIT
GO
After fixing 5120, you can process to fix "databases are in recovery pending state"
Recovery Pending – If the SQL Server knows that database recovery needs to be run but something is preventing it from starting, the Server marks the db in ‘Recovery Pending’ state. This is different from the SUSPECT state because it cannot be said that recovery is going to fail – it just hasn’t started yet.
Execute the following set of queries:
ALTER DATABASE [DBName] SET EMERGENCY; GO
ALTER DATABASE [DBName] set single_user GO
DBCC CHECKDB ([DBName], REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS; GO
ALTER DATABASE [DBName] set multi_user GO
Note: You can also read the Microsoft Warning on DBCC CHECKDB REPAIR ALLOW DATA LOSS.
It might be because of following possible causes:
Permissions
Find your SQL Server instance in the services list and double-click it, then select the Log On tab. It is this log on account that must have sufficient permissions to write to the temporary backup folder location. Check the permissions on the temporary backup folder by right-clicking it in Windows Explorer, selecting Properties, then navigating to the Security tab. Make sure that the account SQL Server is using has explicit read/write permissions for this folder.
Mapped Drives
Use a fully qualified UNC path instead of a mapped drive letter.
Lack Of Domain Trust
You can resolve this issue by ensuring that a trust between the two domains is established. You may also need to configure the SQL Server service account with pass-through authentication between the domains.
Please refer more here for recovery db
Execute these queries to fix SQL server database in recovery pending state:
ALTER DATABASE [DBName] SET EMERGENCY
GO
ALTER DATABASE [DBName] SET single_user
GO
DBCC CHECKDB ([DBName], REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS
GO
ALTER DATABASE [DBName] SET multi_user
GO
EMERGENCY mode marks the SQL Server database as READ_ONLY, deactivates logging, and gives the permission to system admin only. This method is capable of resolving any technical issue and bringing the database back to the accessible state. The database will automatically come out of the EMERGENCY mode.

SQL Job Agent DB Restore fails with error #6107: Only user processes can be killed

We have an SQL Job Agent that runs in the "wee hours" to restore our local database (FooData) from a production backup.
First, the database is set to SINGLE_USER mode and any open processes are killed. Second, the database is restored.
But the 3rd step fails occasionally with Error 6107: "Only User Processes Can Be Killed"
This happens about once or twice a week at seemingly random intervals. Here is the code for step 3 where the failure occasionally occurs:
USE master;
go
exec msdb.dbo.KillSpids FooData;
go
ALTER DATABASE FooData SET MULTI_USER;
go
Does anybody have any ideas what might be occurring to cause this error? I'm thinking there might be some automated process starting up during step 3 or possibly some user trying to log in during that time? I'm not a DBA, so I'm guessing at this point, although I believe that a user should not be able to log in while the DB is in SINGLE_USER mode.
A user probably isn't logged in. The system is probably performing some task. The output of exec sp_who or sp_who2 will show what sessions are open. Any SPID below 50 is a system process, and cannot be killed with KILL. The only way to stop them is to stop the SQL Server service or issue a SHUTDOWN command (which does the same thing).
I found the answer to my problem by changing one line of code which worked like a charm.
As mentioned in the original question, the 'KillSpids" line is used in Step 1 of the job. (Along with SET SINGLE USER) The 'KillSpids' made sense in Step 1 because there may be unwanted processes still active on the database.
The 'KillSpids' line was then added again into Step 3, but it was unnecessary, and was also causing the 6107 error.
I replaced the 'KillSpids' line with the one shown below. Setting the freshly restored database to single user mode takes care of the concern that a user might try to log in before all the job steps have been completed. Here is the updated code:
USE master;
go
ALTER DATABASE [FooData] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
go
ALTER DATABASE FooData SET MULTI_USER;
go

Resources