Unable to attach database after system failures - sql

Without going into too much detail, I had a system failure which meant that SQL got shut down unexpectedly. I have managed to detach the database and remove it from the list of databases in SSMS.
I am now trying to re-attach the database from a different location, on the same NAS;
The command I am using is this;
CREATE DATABASE ArchiveManager
ON (Filename = '\\411352-web5\m$\FTP\Detroit\ArchiveManager.mdf'),
(Filename = '\\411352-web5\m$\FTP\Detroit\ArchiveManager_log.ldf')
FOR ATTACH;
Originally, the database was sat on the share j$. I have now had to move it to m$.
I get the below error why trying to attach the database;
File activation failure. The physical file name "\\192.168.200.222\j$\FTP\Detroit\ArchiveManager_log2.ldf" may be incorrect.
The log cannot be rebuilt because there were open transactions/users when the database was shutdown, no checkpoint occurred to the database, or the database was read-only. This error could occur if the transaction log file was manually deleted or lost due to a hardware or environment failure.
Msg 1813, Level 16, State 2, Line 7
Could not open new database 'ArchiveManager'. CREATE DATABASE is aborted.
Note that 192.168.200.222 resolves to 411352-web5.

Related

Backup database - SQL Server

I need make a backup of my SQL Server database. When I try, I get this error:
System.Data.SqlClient.SqlError: Read on "c:..." failed: 23(...)(Data error (cyclic redundancy error))
Now, I'm trying to run this command:
DBCC CheckDB ('MYDATABASE') WITH NO_INFOMSGS, ALL_ERRORMSGS
But I get this error
Msg 8921, Level 16, State 1, Line 18
Check terminated. A failure was detected while collecting facts. Possibly tempdb out of space or a system table is inconsistent. Check previous errors.
What can I do? I just need make a backup.
I'm using Microsoft SQL Server Management Studio.
First of all, check the Service Account used for the SQL Server Instance from Services.
Ensure the service account have enough permission for read/write at the exact location for Backup in Physical Disk.
Ensure the the user (the user you using to login in SQL Instance) have enough permission to perform backup.
Final option to recover the data from the database is create another database with same tables (blank) in different machine in different SQL instance, then Export all the database to new database using Management studio (Right click on the Database > task > Export Data)

Dynamic restore multiple databases from a folder

I am tasked with restoring several SQL Server databases from .bak files generated from another server in another organization. I need to be able to add automation to this process as it will have to occur daily.
The challenge I am having is that the .bak files have different names every day, like: DatabaseName_Full_Backup_Date_Time_xxxxxx.bak. Also the data and log files need to be moved (MOVE) during the restore to my server's data and log file locations.
I decided to use T-SQL, hoping to find a robust solution that could be implemented from a SQL Server Agent scheduled job that restores the databases, applies the original permissions, and moves\drops the old .bak files to an archive location.
I tried solving this with PowerShell but the -relocate file did not work. Also tried this stored procedure (dbo.spRestore_All_Backups_From_Folder) proposed on this website here which is very close to what I need but in my SQL Server 2016 machine, I get an error when restoring backups generated from another server.
The script works with .bak files from databases that have been backed up on the same server and no MOVE of files is needed.
The error I get when I attempt a restore of a .bak file from another server is:
Msg 213, Level 16, State 7, Line 300
Column name or number of supplied values does not match table definition.
Msg 3013, Level 16, State 1, Line 300
RESTORE FILELIST is terminating abnormally.
Thank you for all input and advice!

how can i attach an MDF file to a server with the same LDF name?

I have a .MDF backup of a database. I need some information from this .MDF.
When I try to attach this .MDF, it requires the log file with which it shipped.
I don't need the log file, but it insists on the .LDF file.
I tried to point it to the same log file with which it shipped, but I am getting the error:
(Since it is in use by the current version of the database.)
I am unable to attach it to a different server because it was originally on a SQL Server 2012, and that is my only SQL Server 2012 server.
How do I reattach the .MDF without the .LDF?
I am adding a text version in case somebody else is searching for a solution to this issue:
Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file "D:\SQL Logs....Custom_log.ldf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)".
File activation failure. The physical file name "D:\SQL Logs....Custom_log.ldf" may be incorrect.
The log cannot be rebuilt because there were open transactions/users when the database was shutdown, no checkpoint occurred to the database, or the database was read-only. This error could occur if the transaction log file was manually deleted or lost due to a hardware or environment failure.
Msg 1813, Level 16, State 2, Line 1
Could not open new database 'TestDb'. CREATE DATABASE is aborted.
I don't know if this is relevant, but I actually do not need to attach .MDF file if I don't have to. I only need to read one table from there.
if you use 2012
use "FOR ATTACH_FORCE_REBUILD_LOG"
CREATE DATABASE [XXXdatabasname] ON
( FILENAME = N'databaseXXXFilePath.mdf' )
FOR ATTACH_FORCE_REBUILD_LOG
You can attach an MDF file without it's corresponding LDF file. To do this, I would use sp_attach_single_file_db

Prevent MS SQL 2005 master DB from being corrupted

I am trying to resolve what cause the following corruption.
2011-06-29 10:47:26.42 spid5s Starting up database 'master'.
2011-06-29 10:47:26.53 spid5s Error: 9003, Severity: 20, State: 1.
2011-06-29 10:47:26.53 spid5s The log scan number (216:72:1) passed to log scan in database 'master' is not valid. This error may indicate data corruption or that the log file (.ldf) does not match the data file (.mdf). If this error occurred during replication, re-create the publication. Otherwise, restore from backup if the problem results in a failure during startup.
2011-06-29 10:47:26.53 spid5s Cannot recover the master database. SQL Server is unable to run. Restore master from a full backup, repair it, or rebuild it. For more information about how to rebuild the master database, see SQL Server Books Online.
I can find plenty of threads and information on how to recover databases when master db is corrupt. I can recover them succussfully.
HOWEVER, this is not very satisfactory for customers to have perform these operations. I have been able to examine event log files of when the corruption occurs. From there I can see server working fine then computer is shutdown, few hours later the computer is switched on and master db is corrupted.
Any help greatly appreciated
One of:
disk corruption. Run chkdsk etc with SQL Server shutdonw
someone has been playing with the MDF/LDF files
The master DB starts once when SQL Server starts up: so why did this happen? Patch? BSOD? PEBKAC? Note: the MDF/LDF files won't be locked when SQL is shutdown...
I can't recall a corrupt master, ever, unless it's one of the 3 reasons above

Duplicate Schema

I am creating and maintaining my SQL Server 2008 warehouse database using a Visual Studio 2008 project.
When I try to deploy the project, I get an error as below
Creating DataWarehouseReports...
Company.HigherEducation.DataWarehouse.dbschema(0,0)Error TSD01268: .Net SqlClient Data Provider: Msg 2714, Level 16, State 6, Line 2 There is already an object named 'DataWarehouseReports' in the database.
Company.HigherEducation.DataWarehouse.dbschema(0,0)Error TSD01268: .Net SqlClient Data Provider: Msg 2759, Level 16, State 0, Line 2 CREATE SCHEMA failed due to previous errors.
An error occurred while the batch was being executed.
Done executing task "SqlDeployTask" -- FAILED.
Done building target "DspDeploy" in project "Company.HigherEducation.DataWarehouse.dbproj" -- FAILED.
Done executing task "CallTarget" -- FAILED.
Done building target "DBDeploy" in project "Company.HigherEducation.DataWarehouse.dbproj" -- FAILED.
Done building project "Company.HigherEducation.DataWarehouse.dbproj" -- FAILED.
What I found, is that the DataWarehouseReports reports schema exists in the master database (granted, by mistake), but I get the "duplicate" error when I try to create it in the OLAP database.
In development, my OLAP and OLTP databases are on the same server, and I encounter the same problem for schema names. For now, I have named schemas differently in the OLTP vs. the OLAP, but for consistency sakes, I would rather have the same name.
How do I debug this?
edit: PEBCAC Alert
I found out that I have a pre-deployment sql script that changes the database context to MASTER. My script was failing and therefore the context never was changed back.
So when the rest of my script executed, it was actually trying to create everything in Master.
Lesson learned: Read the output when the project Deploys.
Basically, the VS2008 project creates a .SQL file and in which the change DB is only where the DB gets created. If you change context in the Pre-Deployment, it is up to you to change it back.