Thursday, September 16, 2010

Database Free Space Details

Database Free Space

I was thinking to prepare this script and finally I did. This script will give database size and free space details.

SQL 2005 and 2008

create table #DBSpaceDetails (DBName nvarchar(150), Name nvarchar(100), PhysicalName nvarchar(1000), TotalSize bigint, FreeSpace bigint)

insert into #DBSpaceDetails (DBName, Name, PhysicalName, TotalSize, FreeSpace)
exec sp_msforeachdb 'use ?; SELECT db_name(), name, physical_name, size/128 AS ''TS (MB)'', size/128.0 - CAST(FILEPROPERTY(name, ''SpaceUsed'') AS int)/128.0 AS ''AS (MB)'' FROM sys.database_files'

select * from #DBSpaceDetails
Drop table #DBSpaceDetails

Note: size (8 KB page) is unit number and we need to convert this value to actual MB.
1024 KB = 1 MB
1 Unit = 8 KB
10 Unit = 1280 MB (ie 10 * 128 = 1280 MB; 1024/8 =128)

Script for SQL 2000

create table #DBSpaceDetails (DBName nvarchar(150), Name nvarchar(100), PhysicalName nvarchar(1000), TotalSize bigint, FreeSpace bigint)

insert into #DBSpaceDetails (DBName, Name, PhysicalName, TotalSize, FreeSpace)
exec sp_msforeachdb 'use ?; SELECT db_name(), name, filename, size/128 AS ''TS (MB)'', size/128.0 - CAST(FILEPROPERTY(name, ''SpaceUsed'') AS int)/128.0 AS ''AS (MB)'' FROM sysfiles'

select * from #DBSpaceDetails
Drop table #DBSpaceDetails

Output Result

Wednesday, August 25, 2010

Restore multiple data and log file script

Most of the databases (All) were multiple data and log files in my production server. Monthly once, I need to refresh whole databases from production to staging and testing servers. Each and every time, I will get physical files and logical name from backup files and prepare manual custom restore script. So I created a script which will generate restore script.

Restore Script for existing database with same number Physical and logical files.

use mytestdb

Declare @sSQL varchar(8000)

set @sSQL ='restore database '+ db_name () +' from disk =''\\\sql_backup\WILSQLP14\SQL2005\ilcorpdb.bak'' ' + char(13) + 'with '

select @sSQL = @sSQL + 'move ''' + name + ''' to ''' +physical_name+''', ' + char(13) from sys.database_files where TYPE =0

select @sSQL = @sSQL + char(13) + 'move ''' + name + ''' to ''' +physical_name+''',' from sys.database_files where TYPE =1

set @sSQL = LEFT (@ssql, len(@ssql)-1)

select @sSQL

Sample Result

restore database MyTestDB from disk ='h:\MyTestDB.bak'
with move 'MyTestDB_Data' to 'F:\SQL2005\SQLData\MyTestDB.mdf',

move 'MyTestDB0' to 'F:\SQL2005\SQLData\MyTestDB_1.ndf',
move 'MyTestDB1' to 'F:\SQL2005\SQLData\MyTestDB_2.ndf',

move 'MyTestDB2' to 'F:\SQL2005\SQLData\MyTestDB_3.ndf',
move 'MyTestDB3' to 'F:\SQL2005\SQLData\MyTestDB_4.ndf',
move 'MyTestDB4' to 'F:\SQL2005\SQLData\MyTestDB_5.ndf',

move 'MyTestDB5' to 'F:\SQL2005\SQLData\MyTestDB_6.ndf',
move 'MyTestDB6' to 'F:\SQL2005\SQLData\MyTestDB_7.ndf',

move 'MyTestDBIndex0' to 'F:\SQL2005\SQLData\MyTestDB_8.ndf',

move 'MyTestDBIndex1' to 'F:\SQL2005\SQLData\MyTestDB_9.ndf',
move 'MyTestDBIndex2' to 'F:\SQL2005\SQLData\MyTestDB_10.ndf',

move 'MyTestDB_Log' to 'G:\SQL2005\SQLLog\MyTestDB_Log.ldf'

Restore Script for fresh database.

Before execute below script, "Ad Hoc Distributed Queries" should be enabled else you will get below error.

Msg 15281, Level 16, State 1, Line 10

SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.

Msg 208, Level 16, State 1, Line 19

Invalid object name 'tempdb..tBakFileContent'.

Enabling "Ad Hoc Distributed Queries"

sp_configure 'Ad Hoc Distributed Queries', 1

reconfigure with override

Restore script

Declare @sTargetDataLocation varchar(5000)

Declare @sTargetLogLocation varchar(5000)

Declare @sSQL varchar(8000)

Declare @sDBName varchar(1000)

set @sTargetDataLocation = 'D:\SQLData\'

set @sTargetLogLocation = 'L:\SQLLog\'

set @sDBName = 'MyTestDB'

select LogicalName, right(PhysicalName, CHARINDEX ( '\', REVERSE (PhysicalName),1)-1 )PhyName , Type

into tempdb..tBakFileContent


openrowset('SQLNCLI', 'server=SQLServer01\inst1;Trusted_Connection=yes;',


exec (''restore filelistonly from disk =''''h:\MyTestDB.bak'''''')')as a

set @sSQL ='restore database ' + @sDBName + ' from disk =''h:\MyTestDB.bak'' ' + char(13) + 'with '

select @sSQL = @sSQL + 'move ''' + LogicalName + ''' to ''' +@sTargetDataLocation+ PhyName+''', ' + char(13)

from tempdb..tBakFileContent where TYPE ='D'

select @sSQL = @sSQL + char(13) + 'move ''' + LogicalName + ''' to ''' +@sTargetLogLocation+ PhyName+''','

from tempdb..tBakFileContent where TYPE ='L'

set @sSQL = LEFT (@ssql, len(@ssql)-1)

select @sSQL

drop table tempdb..tBakFileContent

Monday, August 2, 2010

SQL 2008 R2 Setup Error -'' is not a valid login or you do not have permission.

This time i had face another one issue. When I was trying to install SQL 2008 R2 enterprise edition on my laptop i got below error.

The following error has occured:
'' is not a valid login or you do not have permission.

This error occurs when system name and login names are same. My laptop and login names are same.
This error occurs after adding my login accounts in "Specify SQL Server administrators" on "Database Engine Configuration" tab.

How to get rid this error?

We can achieve this goal using 2 methods.
1. Login in two another windows login account and install.
2. Do not add your account and add another account which name is differ from system. I have used VM.

I have tried both methods and succeeded.

Sunday, June 13, 2010

SQL 2000 to SQL 2008 Upgrade

This time I had faced very strange issue. Setup closed automatically with out any waring or error after setup support file installation. There is no error in event log and setup boot strap folder. I had tried SP1 slipstream but setup throws different error.

So, I had upgraded SQL 2000 to 2005 without any issues and was trying to upgrade SQL 2005 to 2008 but setup closed once support file installation completed.

Setup closed after setup support file so I had installed setup support file from SP1. First we need to extract setup file using below statement.

Extract Command
SQLServer2008SP1-KB968369-x86-ENU.exe /x:T:\SP1

Once extracted, just run support setup file which is located in below mentioned path.


Now setup moved to next step. But middle of the setup I got below error.


The specified local group already exists. (Exception from HRESULT:0x80070563)
Click 'Retry' to retry the failed action, or click 'Cancel' to cancel this action and continue setup.

I just pressed 'Cancel' and continue the setup but setup finished incompletely. I had tried setup repair option but same error.

What is the issue?. Setup was trying to re create the local Windows SQL related groups but failed. I removed below mentioned groups in Windows group.


Restarted the repair activity, now I got the error "No mapping between account names and security IDs was done". Setup was trying to map previously created Local groups to SQL server but failed. Pressed 'Cancel' button and continue the setup. Few times, the error was appeared. Finally upgrade completed successfully. Windows local groups were created automatically.

Friday, June 11, 2010

SSIS Package Issues in SQL 2008

After SQL 2000 to SQL 2008 upgrade in cluster environment, SSIS service didnt install automatically. Upgrade wont install SSIS package while upgrade. It should be installed manually on both node manually. Install standalone SSIS service on both node and set auto start.

As per Microsoft recommendation, SSIS service should not be in cluster resource but we can add resource.

After installation, i had faced the below issues.

Issue 1

failed to retrieve data for this request.

After installation, i had faced issues while trying to connect the SSIS package in cluster environment.

By default, SSIS package is always trying to connect the default instance in the node. I had cluster environment so we need to modify configuration file in SSIS package configuration file.

you may also get below errors

Failed to retrieve data for this request. (Microsoft.SqlServer.SmoEnum)

The SQL Server specified in Integration Services service configuration is not present or is not available. This might occur when there is no default instance of SQL Server on the computer. For more information, see the topic "Configuring the Integration Services Service" in SQL Server 2008 Books Online.

Login Timeout Expired

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2008, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.

Named Pipes Provider: Could not open a connection to SQL Server [2]. (MsDtsSvr).


1. Navigate this location \Program Files\Microsoft SQL Server\100\DTS\Binn\
2. Edit MsDtsSrvr.ini.xml in notepad and change server name "." to Cluster name.

< xsd="" xsi="">
<>true < /stopexecutingpackagesonshutdown>
< type="SqlServerFolder">
<>MSDB < /name>
<>ServerName\InstanceName < /servername>
< /folder>
< type="FileSystemFolder">
<>File System < /name>
<>..\Packages < /storepath>
< /folder>
< /toplevelfolders>
< /dtsserviceconfiguration>

3. Save XML file and restart SSIS service

Issue 2
"Connect to SSIS Service on machine failed: Access is denied"

While trying to connect the SSIS package from client machine. I can able to connect the SSIS service in node.

We need to add below steps in Local group policy and windows component service.

1. Add users in Distributed COM local windows Users group
2. Start -> Run -> %windir%\system32\Com\comexp.msc to launch Component Services
or program files -> Administrative tools -> Component Services
3. Expend Component Services\Computers\My Computer\DCOM Config
4. Right click on MsDtsServer node and select properties
5. Navigate Security page menu
6. Click Edit button in “Launch and Activation Permissions” section.
7. Add users and provide all permissions. Press ok to complete.
8. Click Edit button on “Launch Permissions” section
9. Add users and provide all permissions. Press ok to complete.
10. Restart SSIS Service

Wednesday, June 2, 2010

Cluster service failed to start local system account

After windows cluster, the windows cluster service failed to start. Error indicate that cluster service didnt strat on local system account.
Error Message

Could not start the Cluster Service service on Local Computer.
Error 1314: A required privilege is not held by the client.

Solution If we want to run cluster service under domain account or local admin group, service account should be added in below locations.

Open Local Security Policy on Administrator group and configure below locations.

Local Security Policy\Security Settings\Local Policies\User Rights Assignment

* Act as part of the operating system
* Adjust memory quotas for a process
* Back up files and directories
* Increase scheduling priorities
* Log on as a service
* Restore files and directories
* Debug programs
* Impersonate a client after authentication
* Manage auditing and security log

This solution is applicable for windows 2003 server. Refer other version on below link;en-us;Q269229

Tuesday, May 25, 2010

SQL 2008 Upgrade in Cluster Environment Setup Issue

Cooool feature

SQL 2005 to SQL 2008 upgraded in cluster environment is very very interesting and fantastic. Yes, it is really amazing. There are no downtime required for SQL 2008 upgrade. Start upgraded from passive mode and finally move active node to passive node and upgrade. For example environment has 3 cluster node with named clusa, clusb, clusc nodes. clusa is active remaining are passive node. Apply upgrade in passive node one by one. Setup wont upgrade all instances simultaneously. during this upgraded SQL server is online and running with SQL 2005 version. Once upgraded is completed in passive node than move cluster resources to passive node. Now you will the big surprise, yes... SQL server is running in clusb with SQL 2008 version. Finally upgrade SQL 2008 on clusa.

Error Message

Cannot execute a program. The command being executed as "c:\WINDOWS\microsoft.NET\Framework\v2.0.50727\csc.exe" /noconfig /fullpaths @"c:\Documents and Settings sqlservices\Local settings\Temp\hin6evfc.cmdline".


IIS admin service is bloking .NET freamwork installation. Just stop IIS admin service in service control manager. Now SQL 2008 upgrade will run without any issues.