Search

Saturday, March 5, 2011

USING OPENXML TO PROCESS CSV (COMMA-SEPARATED VALUE) STRINGS

This may be common knowledge to those among us that are handy with XML (I'm not), but since I just uncovered this nice little trick I thought I'd pass it along either way. It is a very easy way to process a CSV string and turn it into multiple rows:

declare @idoc int -- the handle for the XML document

declare @csv varchar(8000) -- the CSV we are being passed

set @csv = '1,2,3,7,4,8,10' -- our list of CSV values

-- With a simple edit of the CSV string:

set @csv = ''

-- We can now do this:

EXEC sp_xml_preparedocument @idoc OUTPUT, @csv

-- And then:

SELECT *

FROM OPENXML (@idoc, '/root/id',1)

WITH (value int)

This raises some interesting possibilities about ways we can process regular text strings (like CSV's) by using simple

replace techniques to turn that string into an XML value. It is certainly shorter that the usual looping technique to split

a CSV string into each value and then insert that value into a temp table or a table variable. I'm not sure if it's more or

less efficient, though.Anyone else have any other nice tricks like this?

Friday, March 4, 2011

Connect to SQL Server from command prompt – list tables and database

Q. How do I connect to Microsoft SQL Server from command prompt? I just wanted to see list of tables and database.

A. MS- SQL Server is relational database management system.

Its primary query language is Transact-SQL, an implementation of the ANSI/ISO standard Structured Query Language (SQL) used by both Microsoft and Sybase.

There is command line tool available and it is called as sqlcmd. you need to enter the word GO after every command.
On the Start menu click Run. In the Open box type cmd, and then click OK to open a Command Prompt window.
At the command prompt, type sqlcmd.

Press ENTER.

Let us say your username is vivek and password is foo, use:
C:> sqlcmd -U vivek -P foo

Once connected you should see 1> prompt. Type following command to use database called sales:

use sales
GO

To list tables type:

sp_help

OR

select * from SYSOBJECTS where TYPE = 'U' order by NAME

To List all the databases on the server:

sp_databases

To list fields in a table called foo:

sp_help tablename
sp_help foo

How to Repair and Restore Indexes of the SQL Server Database

In MS SQL Server database, an index is the data structure that improve the speed of operations on table. Database indexes are created by the using of single or multiple columns of table that offer efficient access and random lookups of ordered records. That means indexes of SQL Server database improve the management of data and enhance the SQL Server performance. But in case if index of SQL Server database gets corrupted, you might come across in serious data loss situation. At this situation you need to repair and recover corrupted SQL Server database by using SQL database recovery software to restore the database. Consider a practical scenario, when you try to access Master database file of microsoft SQL Server database, you might come across below mention error message: "Possible index corruption detected. Run DBCC CHECKDB." After appearing the above error message, you cann't do any activity on the SQL Server database. In order to gain access of your important data in the database, you have to find-out the exact cause of this error message and go forward for SQL database repair by sorting it out. Cause of the Problem: The above error message is an event log error message, which appears when Master database file or components of master database file gets corrupted. The database might get damaged or corrupted due to various reasons such as hardware problem, virus infection, registry errors, software malfunction and many more. Solutions of the Problem: For resolving the above error message you have to check the hardware of the system, if hardware is ok then run DBCC CHECKDB command with the proper clause. After the running the command you have still not able to access your database then restore it from the latest backup if backup is updated. If backup is not available then you have to try a third party SQL Recovery software to fix the problem. The best third party SQL recovery software recommended by the most database expert is Stellar SQL database recovery. This software enables you to repair MDF as well as NDF file of SQL Server database. SQL Recovery software supports MS SQL Server 7.0, 2008, 2005 and 2000 and compatible with Microsoft Windows 7, Vista, 2003, XP, 2000 and NT.

Thursday, March 3, 2011

How to Solve Error 8944 in MS SQL Server Database to recovery Corrupt MDF File?

Database corruption in SQL databases can amount to huge losses depending upon the size of your organization. Because of database corruption, you would not be able to access the databases, work on them, and most importantly all the stored records would become vulnerable. But there are certain in-house workarounds that would enable you to perform mdf recovery. However, if these methods do not work then you should use a third-party SQL database recovery software to repair the damaged database.

Consider a situation in which you are facing the following error message while working on an MS SQL Server 2000 database:

Table error: Object ID O_ID, index ID I_ID, page P_ID, row S_ID. Test (TEST) failed. Values are VAL1 and VAL2.”

Cause:

The most prominent reason for this table error is that the a record has failed while auditing as corruption is detected in its record header.

Resolution:

To overcome this problem, there are following methods that you should try, preferably in the given order, to recover MDF file:

  • Addressing hardware-related errors: You should check and address the hardware-related issues that may have caused the database header corruption. For this purpose, you can perform the following measures:

    • Check the Application log and Error log in SQL Server to ascertain whether the error has occurred because of hardware corruption.

    • Fix the errors that appear after running hardware diagnostics.

    • Swap the hardware components to isolate the actual reason of database corruption.

    • Reinstall operating system after formatting the hard disks.

  • Restoring database using backup: You should check the status of the database backup. If it is clean and updated, then restore the database using the backup.

  • Running DBCC CHECKDB: You should run DBCC CHECKDB with the suggested repair clause to repair the damaged database.

If you still face the same error message even after performing these steps, that means the database is severely damaged. To perform MDF file recovery in such cases, you should use a third-party master database recovery software. These read-only tools are very safe to use that do not overwrite the repairs on the original database.

Tuesday, March 1, 2011

Rectifying Error 926 in MS SQL Server 2008 database

Are you facing problems in mounting a MS SQL Server 2008 database? Then, there are chances that the database (MDF file) is corrupt, which is why you are unable to access it. Such scenarios can occur due to various causes such as virus infections, power outages, damaged hardware components, etc. In such cases, you should find out the exact reason for database corruption and try to repair it. However, if you are not able to do so, then you should use a third-party SQL Server recovery software to perform MDF file recovery.

Consider a scenario wherein you have installed MS SQL Server 2008 on your system. When you try to open a particular database, it fails to open. The following error message is displayed, as a result.

Database '%.*ls' cannot be opened. It has been marked SUSPECT by recovery. See the SQL Server error log for more information.”

Cause:

The root cause for such situation is that the database is marked as suspect, as is suggested in the message, and is corrupt. This is evident from the fact that it has not passed the recovery process that would have bring the database to a consistent state. It could have corrupted during the following situations:

  • While starting the SQL Server instance

  • When you were attaching a database

  • When you were using the RESTORE database or RESTORE LOG procedures

Resolution:

To resolve this problem, you should perform the following methods:

  • Check error log: You should check the error log and try to find out the exact cause of the error. Also, if SQL Server started after the failed recovery then check the previous error logs to know the reason of recovery failure.

  • Look for hardware issues: You should check if the problem has occurred because of hardware malfunctioning such as regular I/O errors, damaged page, etc. In such cases, you should replace the hardware component and try to mount the database again.

  • Restore from backup: If you have a clean and updated database backup, then you should restore the database from its backup.

  • Run DBCC CHECKDB: If the backup is not clean or updated, then you should run the DBCC CHECKDB command, first without the repair clause to determine the extent of corruption. Then, you should run it with the suggested repair clause to repair the corrupted database.

However, if you still are not able to mount the SQL Server database even after performing the aforementioned methods, then you should use a third-party master database file recovery software to recover MDF file. Such SQL recovery tools are read-only in nature that do not overwrite the existing database while scanning.