SansSQL

Tuesday, September 25, 2012

T-SQL Query to find List of Tables that do not have Primary Key

We know that Primary Key is a must for setting up articles in Transactional Replication.
This query list those tables which do not have Primary Key in that database.

USE <DatabaseName>
GO
SELECT SCHEMA_NAME(schema_id) AS [Schema Name], name AS [Table Name]
FROM sys.tables
WHERE OBJECTPROPERTY(OBJECT_ID,'TableHasPrimaryKey') = 0
Order by name
GO

Thursday, September 20, 2012

Update statistics for all user database

In one of my previous post "What is Statistics in SQL Server", I had explained about what is Statistics.
Today, I am posting a Query which will update statistics on all user database in that Instance.

EXEC sp_MSForeachdb 'USE [?];
IF ''?'' not in (''master'',''model'',''msdb'',''tempdb'',''distribution'') 
 AND DATABASEPROPERTYEX(''?'',''Updateability'') = ''READ_WRITE''
BEGIN
Print ''Updating statistics for database "'' + ''?'' + ''"''
EXEC sp_updatestats
END'

This Query will exclude the system database and those database which are not in Read_Write status.

Tuesday, September 11, 2012

Database Corruption and Recovery

What is database corruption?
Inconsistency in the internal structure of the database with respect to data or log files is known as database corruption

What causes database Corruption?
  • Physical Inconsistency - One or more access paths to the data may be invalid.
  • Logical Inconsistency  - One or more pointers to the data may be invalid

How to detect Corruption?
  • Status of database set to “suspect”
  • Evident from Error log and/or Event Viewer logs
  • Consistency errors from DBCC CHECKDB
  • T-SQL Queries or application throwing corruption related error (Database might be online without being suspect)
  • SQL Server Startup Failure
  • Failure while Restoring or Attaching databases

What are the causes that leads to database corruption?
  • Hardware failure event
    • Power outage
    • SAN crash
    • NTFS or File System Corruption
    • FTDisk Errors
    • Bad Block or Disk Corruption
    • Outdated  or Faulty Drivers
  • Failed Restore/Attach
    • Due to Bad File/Page
    • Abnormal Termination of the process
    • Corrupted Header
  • User Error
    • File Deletion/Renaming
    • File Swapping
  • 3rd Party Software
    • Filter Drivers
    • Outdated/Faulty Device Driver

Recovery Flows
  • master Database
  • model Database
  • msdb Database
  • tempdb Database
  • User Database

Wednesday, August 29, 2012

Guidelines for choosing antivirus software to run on the computers that are running SQL Server

These are the very general guidelines to help you decide which kind of antivirus software to run on the computers that are running Microsoft SQL Server and I personally recommend to perform some testing before and after the installation of antivirus to determine if there are any performance issues as the antivirus software will utilize some system resources to perform their duties.

To drill own, basically there are two kinds of servers, one is High Risk Servers which are generally exposed to public internet or which have open ports to the servers that are not behind firewall or which hosts file shares or HTTP services like IIS or Apache.
And the servers which do not meet the above criteria of High Risk servers will fall under the category of Low Risk Server although not always.

When you configure antivirus software on a server running SQL Server, make sure to exclude the following

File Extensions and Directories

  • SQL Server database files
    • .mdf
    • .ndf
    • .ldf
  • SQL Server backup files
    • .bak
    • .trn
  • Full-Text catalog files
    • Default instance: Program Files\Microsoft SQL Server\MSSQL\FTDATA
    • Named instance: Program Files\Microsoft SQL Server\MSSQL$instancename\FTDATA
  • Trace Files
    • .trc
  • Audit files
    • .sqlaudit
  • Query files
    • .sql
  • Directory that holds Analysis Services Data and Temporary files
  • Directory that holds Analysis Services Log files
  • Analysis Services backup files

Processes

  • SQLServr.exe - Process related to SQL Server Database engine
  • ReportingServicesService.exe  - Process related to SQL Server Reporting Services
  • MSMDSrv.exe  - Process related to SQL Server Analysis Services
We can also run antivirus software on a SQL Server cluster. For this we have make sure that the antivirus software we choose supports cluster. 
When running antivirus on cluster make sure to exclude 
  • Q:\ (Quorum drive)
  • C:\Windows\Cluster
Doing this improves the performance of the files and helps make sure that the files are not locked when the SQL Server service must use them. However, if these files become infected, the antivirus software cannot detect the infection. So if you suspect a virus infection then you have to scan the entire system without any exclusions.

Monday, August 27, 2012

Fix SSMS Error - The automatically saved settings file ‘\\Settings\CurrentSettings-YYYY-MM-DD.vssettings’ cannot be found.

When you try to Open or Close SQL Server Management Studio after an upgrade or fresh installation of SQL Server 2008 R2 you might get the below error which states,
The automatically saved settings file '\\Settings\CurrentSettings-YYYY-MM-DD.vssettings' cannot be found. You can change this file on the 'Import and Export Settings' Tools Options page. The IDE will use your most recent  settings file for this session.


However there is no option "Import and Export Settings" in SQL Server Management Studio.
So to Fix this error,
  1. Open Registry Editor
  2. Navigate to the location HKEY_CURRENT_USER\Software\Microsoft\Microsoft SQL Server\100\Tools\Shell
  3. Edit the value of the Reg Key "VisualStudioLocation" to  %USERPROFILE%\Documents\SQL Server Management Studio
  4. Close Registry Editor
Now when you open or close SQL Server Management Studio, You will not get the error.

Ads