This is my procedure for "rescuing" data from a corrupted database. Obviously restoring from backup is a lot easier!
0) Set the damaged database to Read-Only. if you don't have a backup make one now.
1) Script the database
2a) Create a new, TEMP database - preferably on a different machine in case of hardware problems on the original machine
2b) Size the Data for the TEMP database same size as the original (to avoid dynamic extensions). Size the Log something large-ish!
3) Run the Script on the TEMP database. Do NOT create any FK etc. yet
4a) Attempt to transfer all tables:
-- Prepare script of: INSERT INTO ... SELECT * FROM ...
SET NOCOUNT ON
SELECT 'PRINT ''' + name + '''' + CHAR(13) + CHAR(10) + 'GO' + CHAR(13) + CHAR(10)
+ CASE WHEN C.id IS NULL
THEN ''
ELSE 'SET IDENTITY_INSERT dbo.[' + name + '] ON' + CHAR(13) + CHAR(10)
END
+ 'INSERT INTO MyTempDatabase.dbo.[' + name + ']' + CHAR(13) + CHAR(10)
+ 'SELECT * FROM dbo.[' + name + ']' + CHAR(13) + CHAR(10)
+ CASE WHEN C.id IS NULL
THEN ''
ELSE 'SET IDENTITY_INSERT dbo.[' + name + '] OFF' + CHAR(13) + CHAR(10)
END
+ 'GO'
FROMdbo.sysobjects AS O
LEFT OUTER JOIN
(
SELECT DISTINCT C.id
FROMdbo.syscolumns AS C
WHEREC.colstat = 1-- Identity column
) AS C
ON C.id = O.id
WHERE type = 'U'
AND name NOT IN ('dtproperties')
ORDER BY name
SET NOCOUNT OFF
this generates statements like this:
PRINT 'MyTable'
GO
SET IDENTITY_INSERT dbo.[MyTable] ON
INSERT INTO RESTORE_XFER_TEMP.dbo.[MyTable]
SELECT * FROM dbo.[MyTable]
SET IDENTITY_INSERT dbo.[MyTable] OFF
GO
4b) This will give some sort of error on the tables which cannot be copied, and they will need to be rescued by some other means.
5a) Each "broken" table needs to be rescued using an index. Ideally you will have a clustered index on the PK and that will be undamaged, so you can "rescue" all the PKs into a temp table:
-- Copy PK fields to a temporary table
-- DROP TABLE MyRestoreDatabase.dbo.TEMP_RESCUE_PK
-- TRUNCATE TABLE MyRestoreDatabase.dbo.MyBrokenTable
SELECT[ID]=IDENTITY(int, 1, 1),
[IsCopied]=CONVERT(tinyint, 0),
MyPK
INTOMyRestoreDatabase.dbo.TEMP_RESCUE_PK
FROMMyBrokenDatabase.dbo.MyBrokenTable
ORDER BY MyPK
5b) If that is successful you have a list of all the PKs, so can can try to copy data matching those PKs, in batches:
-- If OK then selectively copy data across
-- First Prep. a temp Batch table
-- DROP TABLE MyRestoreDatabase.dbo.TEMP_RESCUE_BATCH
SELECT TOP 1 [ID]=CONVERT(int, NULL), [IsCopied]=CONVERT(bit, 0), MyPK
INTOMyRestoreDatabase.dbo.TEMP_RESCUE_BATCH
FROMMyRestoreDatabase.dbo.TEMP_RESCUE_PK
GO
--
DECLARE@intStartint,
@intStopint,
@intBatchSizeint
-- NOTE: After the first run set these to any "gaps" in the table that you want to fill
SELECT
@intStart = 1,
@intBatchSize = 10000,
@intStop = (SELECT MAX([ID]) FROM MyRestoreDatabase.dbo.TEMP_RESCUE_PK)
SELECT@intStart = MIN([ID])
FROMMyRestoreDatabase.dbo.TEMP_RESCUE_PK
WHERE IsCopied = 0
AND [ID] >= @intStart
WHILE@intStart < @intStop
BEGIN
SET ROWCOUNT @intBatchSize
-- Isolate batch of Keys into separate table
TRUNCATE TABLE MyRestoreDatabase.dbo.TEMP_RESCUE_BATCH
INSERT INTO MyRestoreDatabase.dbo.TEMP_RESCUE_BATCH
SELECTT.*
FROMMyRestoreDatabase.dbo.TEMP_RESCUE_PK AS T
WHERE IsCopied = 0
AND [ID] >= @intStart
AND [ID] < @intStart + @intBatchSize
-- Attempt to copy matching records, for this batch
PRINT CONVERT(varchar(20), @intStart)
INSERT INTO MyRestoreDatabase.dbo.MyBrokenTable
SELECTS.*
FROMMyRestoreDatabase.dbo.TEMP_RESCUE_BATCH AS T
LEFT OUTER JOIN MyRestoreDatabase.dbo.MyBrokenTable AS D
ON D.MyPK = T.MyPK
-- This will try to get the data from the broken table, it may fail!
JOIN MyBrokenDatabase.dbo.MyBrokenTable AS S
ON S.MyPK = T.MyPK
WHERED.MyPK IS NULL-- Belt and braces so as not to copy existing rows
-- Flag the rows just "Copied"
UPDATEU
SETIsCopied = 1
FROMMyRestoreDatabase.dbo.TEMP_RESCUE_PK AS U
WHEREIsCopied = 0
AND [ID] >= @intStart
AND [ID] < @intStart + @intBatchSize
-- Loop round, until done
SELECT@intStart = @intStart + @intBatchSize
END
GO
SET ROWCOUNT 0-- Turn OFF!!
GO
5c) This will copy in batches of 10,000 [you can adjust @intbatchSize depending on table size] until it gets to a damaged part of the table, then it will abort.
Change the @intStart to the last ID number displayed, and reduce @intBatchSize (by an order of magnitude each time) until you have rescued as many records as possible in the first "part" of the table.
5d) Reset the batch size @intBatchSize to 10,000 [or whatever size is appropriate], and increase the @intStart repeatedly until you are past the damaged section - copying will start again, and will abort if there are further damaged sections
5e) Repeat that process until you have rescued as much of the data as is possible
6) Check what is left to be rescued
-- Check amount NOT done:
SELECTCOUNT(*), MIN([ID]), MAX([ID])
FROMMyRestoreDatabase.dbo.TEMP_RESCUE_PK
WHERE IsCopied = 0
--AND [ID] > 123456-- Optionally count items after a "gap"
--
-- Double check that IsCopied set correctly, and the number of records "lost"
SELECTCOUNT(*),
[IsCopied] = SUM(CONVERT(int, IsCopied)),
[IsCopied+Record] = SUM(CASE WHEN IsCopied = 1 AND C.MyPK IS NOT NULL THEN 1 ELSE 0 END),
[IsCopiedNoRecord] = SUM(CASE WHEN IsCopied = 1 AND C.MyPK IS NULL THEN 1 ELSE 0 END),
[IsNOTCopied] = SUM(CASE WHEN IsCopied = 0THEN 1 ELSE 0 END),
[IsNOTCopied+Record] = SUM(CASE WHEN IsCopied = 0 AND C.MyPK IS NOT NULL THEN 1 ELSE 0 END),
[IsNOTCopiedNoRecord] = SUM(CASE WHEN IsCopied = 0 AND C.MyPK IS NULL THEN 1 ELSE 0 END)
FROMMyRestoreDatabase.dbo.TEMP_RESCUE_PK AS T
LEFT OUTER JOIN MyRestoreDatabase.dbo.MyBrokenTable AS C
ON C.MyPK = T.MyPK
--
-- List of "Lost" records
SELECTMyPK
FROMMyRestoreDatabase.dbo.TEMP_RESCUE_PK
WHERE IsCopied = 0
ORDER BY [ID]
You will then have to "find" and rescue the lost records somewhere.
I have a further process using OPENQUERY() to rescue records to fill the gaps in the event that they are available on a remote system - a straight JOIN to get them is going to be far to slow on anything other than tiny tables!
7a) Create the FKs etc. Update the statistics, rebuild indexes, and possibly shrink the Log if it is unrealistically big
7b) Backup and Restore over the original database
7c) DBCC CHECKDB ('MyDatabaseName') WITH ALL_ERRORMSGS, NO_INFOMSGS
Opinions?I've installed all three to try'em out and they all seem to be workingso far, more or less.Log Explorer has given me a couple of errors but I started over andcontinued OK.ApexSQL Log squawked about "redo for delete cannot be generated fortables lacking clustered index". Huh? What the...? We have lots oftables without a clustered index.I read some reports in Red-Gate's technical support forum about theirproduct actually bringing down SQL Server. That did not inspireconfidence.All three install some server-side components like extended storedproc's which I'm not crazy about and I think all three are server orinstance licensed (does anyone actually purchase more than one licensewhen it seems like you can easily move any tran log to be analyzed tothe server the product is installed on?). I don't think I'd want toinstall any of them on a Production server, at least one that I'mresponsible for!Thanks,Martin
i need to update for example the eployee 1111 with employee 3333 but with swap ( take the value of employee 1111 in field- fld2,fld22,fld222 and swap value between employee 3333 in field- fld2,fld22,fld222 )
Code Snippet ---update eployee 1111 with employee 3333 -so if i put the value 2 than ------------------ swap value between 2 employee set empid1= 1111 set empid2=3333 value_swap=2 if value_swap=2 than update fld2,fld22,fld222 with fld2,fld22,fld222 ------------------- take the value of employee 1111 in field- fld2,fld22,fld222 and swap value between employee 3333 --------------------in field- fld2,fld22,fld222
Code Snippet ---update eployee 2222 with employee 1111 -so if i put the value 5 than ------------------ swap value between 2 employees set empid1= 1111 set empid2=2222 value_swap=5 if value_swap=5 than update fld5,fld55,fld555 with fld5,fld55,fld555 ------------------- take the value of employee 1111 in field- fld5,fld55,fld555 and swap value between employee 3333 --------------------in field- fld5,fld55,fld555
Hi, I have a corrupted table wich has some valuable data in it. I found out that there is only one raw is corrupted. How do I find out which row of the table is it? Can any one Help me in this matter Please?
I have several sites which refer to a table in an MS SQL data base on the server.
I'm looking for a good way to check that my tables don't get corrupted over time. It seems that I can't create a duplicate by selecting the individual table and going SaveAs..
Can someone point me to the fool proof method that everyone else already uses, please ?
our sql server databases are on a window cluster server.how to correct this issue or why would this happen? FILESTREAM data container 'M:TessituraDB DataDocuments' is corrupted. Database cannot recover.
A friend of mine called and said that his database was suspect and he detached it and was unable to attach it. The SQL Server Version is 2000 with the latest service packs installed. When I checked it the database (MDF) size was 1MB and the LDF size was 3.8 GB. I was not able to attach the two together and I was not able to attach the database using sp_attach_single_file_db. I did find an old backup of his database and attached that so he can work off of it but it is 1 yr old and he needs recent data. Since the MDF seemed to be blank we cant do much with that, but there seems to be data in the LDF. Is it possible to extract any data from the LDF file?
I'm in the middle of developing a Database for a hospital that measures its audits, inhouse operations, and finance. What we currently have and do everyday is collect data from a large database that is real time with patient data, progess, infomation, etc and dump it into a data warehouse that runs on TSI/Eclipsys. We run reports using a number of programs and dump it into Excel sheets that have charts, reports, etc. This Database for which I'm developing won't come soley from the TSI/Eclypsys source, but this is the only source thats updated regularly. I don't want to have in sync with TSI/Eclipysys in fear that every day when it updates data may be lost, not read, or worse won't be up date if there is a problem. My question is is it possible to run a query from Sequel Server 2005 that will take that data upon request using the reporing features on Sequel Server 2005. i.e. What if I need to run a report on measure B in department 12 from Jan 1-Feb 1, instead of being in sync, can I just write queries to take that information rather than double the data and take up twice the space and trouble. FYI, these datatypes rarely change in the TSI/Eclipsys data warehouse. This sure was long question and didn't intended it to be . Thanks for listening and hope to hear back.
how can i attach my database without the ndf file, i still have the mdf and ldf. my ndf file contains only 2 tables which are not that very impt. unfortunately my ndf was corrupted and my backup files was deleted when the network admin formatted the harddisk where the backups were stored.. is it possible? kindly help..
We have one database of 5 GB of which when we take the backup and restore in another location, it says "Device Activation Error. The Physical file name e:databaseike_log.ldf may be incorrect". The database has more than one log file. Now when we use another procedure to create new database and stop the sql, then replace the old mdf file and REBUILDLOG. (Ie dbcc rebuildlog(nike,1,0)). It says incorrect DBCC Statment.
SQL Server 6.5. Hi! When I try to create PK, Clustered index on the column in the table I have got error message: 1105 Couldn't not allocate space for object table1...... But it is a big amount of free space in the database. And actually I can create any indexes, exsept clustered with no problem. DBCC CHECKALLOCK for this table shows: extent not ih the segment. Does it mean the table structure is corupted? What Can I do to resolve the problem?
When I am trying to restore the backup it is giving an error message "An internal consistency error occurred. Contact Technical Support for assistance".
Please suggest me on this issue.
Is there any way to restore the backup or recover the backup.
I have a SQL 2000 database whose MDF is approx. 105 GB. Everything has been working fine for months until this past weekend when the database server dropped offline. I restarted the services and SQL server looks to have started up okay. However, trying to access it via Enterprise Manager causes EM to hang. Checking the Windows event log, there are a whole series of messages stating ...
3455 : Analysis of database 'ptportal' (7) is XX% complete (approximately 61 more seconds)
This 'analysis' ran for over 12 hours ... I have never seen this run before, much less this long. Now, there are a new series of messages stating ...
3450 : Recovery of database 'ptportal' (7) is XX% complete (approximately 56526 more seconds) (Phase 2 of 3).
This 'recovery' has been running for 9 hours now. Does anybody have any idea what has happened here or what is going on? My guess is that this database has been corrupted and SQL Server is trying to do a repair? Anybody know how long this usually takes and what the success liklihood would be?
this may well be the wrong place for my post, if so please move it.
I have a variety of boxes with SQL tools installed on them. For some reason last week several of them seem to have gone a bit squif.
Server 1. Enterprise manager fails to run, giving message MMC could not create the snap-in. QA runs fine.
Server 2. Load EM click on a database and try Tools --> QA it comes back with File Not Found. Add isqlw as an external tool and you can run that ok, but still File Not found on the QA tab.
I know its not data corruption, but I hope someone can give me a clue on this!
My sql server is not starting becasue master db got corrupted. I have tried to rebuild the master db with rebuild utility.Now the process of rebuilding taken 6 hours but still the process of rebuild master is going on. Can any body faced this problem why it is not rebuilding the simple master database.
Thanks you. BPG
EDIT by tkizer: moved to Data Corruption Issues forum
I have a database corrupted showing on the Object Explorer the name of the database and showing near (6.5). Deleted not accepted any other function not allow. Is there any way to take off from the Object Explorer?
Hello,I have a SS2K (SP3) that is appending very wierd looking error messagesto the SQL Server Log (Current). The messages are not formatted asother log entries. The messages suggest some kinda dump information.The messages have the following characteristics:Date Information is invalidSource information is invalidMessage information is strangeThe following is a message that appear in my current SQL Server LogDATE:78008454SOURCE: Module(MSVCRT+Message (_endthread+000000C1)There are many messages like this in the log.Is this serious?Thanks - Covi
Hey Hey everyone. It's me again asking for help.I have an NT sp6 box with SQL 7. It seems the password for SA hasbeen corrupted, so it won't allow me to log in to it.As an NT admin, is there anyway to reset the SA password?Or, can I create another SA account in SQL 7?Can't find anything in Microsoft because..... SQL7 is not longersupported.Thanks in advance for any help.Jinev
Need to recover from a corrupted log file. The database in EnterpriseManager shows the database as corrupt.Using sp_Attach_db produces a corrupt log message.Any way to recover the data?Thanks
hello, i am using sql server ce 2.0 on windows ce 4.2.sometimes my sdf file gets corrupted but i cant understand why?i know how this file repair.but for me it is not a solution.please somebody help me!
I try to run VB2005 Sample101, "Using Datagridvies" with SQL Express,
Here is what I got !"
"One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. if this is an existing database, the file may be corrupted and should be restored from a back up.
Cannot open use default database. Login failed. Login failed for use 'XXXXXXXXXXXAdministrator" Lof File 'C:Program FilesMicrosoft SQL ServerMSSQL.1DataAdventureWorks_Data_Log.idl' does not match the primary file. It may be from a different database or the log may have been rebuilt previously."
Then I re attached the Database again. Still the Same problem.
I try other sample that used Northwin and Pubs (SQL2000 ) and attached to
We a team of 10 members accessing a particular database with a common user name and password . Some one have deleted the data from a business critical table. Transaction log backup is done every half an hour. Considering backup is taken at 10AM and my data is lost at 10.26AM. How to recover the lost data?
Is it possible to recover the data from log file or any other way to achieve the data recovery?
Is is possible to trace the person who have done this job. (By IP address or any other way)?
I am having problems with my applications files. Part of my windows foundation or plug and play (error 31 - device not attached to system). I also found that my sql server is corrupted and I got the message to uninstall and reinstall. I don't know how to find where to go to re-install and unfortunately I uninstalled the VSS sql already. How do I find my sql server to re-download? help!
I was working away as usual, when BIDS crashed. Upon re-starting, my package tasks no longer had any connectors between them! However, if I selected, then clicked on them, the connectors reappeared again. I thought this was a one-time problem.
However, now other packages, upon opening, are missing the task connectors as well. This has never happened before until this morning after BIDS crashed. It seems to be permanently corrupted at this point.
Should I reinstall SSIS? Has anyone else seen this before?
ok..my friend sent me a video via file transfer, but when i tried to view it, and open it up with windows movie maker (that is the program she made the video on) it said the file was corrupted. is there any way i can fix the corrupted file? or any other program i can view it on? windows media player also declares it corrupted.
Hi All, We currently got two databases in the application(Visual web express with vb script). The one database is a sql 2000 that sits on a server and is fed by people that does E Learning. The app. got its own database(SQL 2005 express) with the record of the people doing the learning and some other attendances from other courses. I would like to determine attendances from both databases etc. How do I link this to table form diffrent databases into one table for me to do training cost etc. Thanks for your help.
I am backing up SQL 6.5 DB using Backup Exec 7.2 on NT 4.0 server SP4 with SQL server agent. The backup job ends in Failed status and a warning that DBname is corrupt. I then try to run DBCC Checkdb, newalloc, and checkcatalog commands to repair the DB. No further ahead yet. Are there any other commands that I can perform on the DB so that the corruption can be repaired, or verified there indeed is corruption of DB?