Tuesday, 26 February 2013

SQL Server - Rebuilding the Master Databse

To change the default collation for SQL Server it is necessary to rebuild the master database. For full details of how to do this check out the MSDN link. The following gives a brief summary of the steps involved.Run the following command from the SQL Server setup media directory:
Setup /QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=MSSQLSERVER /SQLSYSADMINACCOUNTS=domain\account /SAPWD=SApassword /SQLCOLLATION=Latin1_General_CI_AS
Check the location of the physical data files:
SELECT name, physical_name AS current_file_locationFROM sys .master_filesWHERE database_id IN (DB_ID ('master'), DB_ID( 'model'), DB_ID('msdb' ), DB_ID('tempdb' ));
Move the tempdb files if required:
USE master ;GOALTER DATABASE tempdbMODIFY FILE (NAME = tempdev, FILENAME = 'F:\MSSQLData\tempdb.mdf');GOALTER DATABASE tempdbMODIFY FILE (NAME = templog, FILENAME = 'F:\MSSQLLogs\templog.ldf');GO
Once the files have been moved and the service restarted then the tempdb files created during the rebuild can be removed from the old location.You may want to disable the sa account:
ALTER LOGIN sa DISABLE;

Monday, 26 November 2012

SQL 2008 Active Node

The following can be used to determine the active node in a SQL cluster
SELECT ServerProperty('ComputerNamePhysicalNetBIOS')

Friday, 16 November 2012

Transfering SQL 2008 Logins Between Servers

The following script can be used when transfering logins between SQL Server instances whilst preserving the password. This has been tested on SQL 2008 only. 1. create the sp_help_revlogin stored procedure using the following.
USE master GO IF OBJECT_ID ('sp_hexadecimal') IS NOT NULL DROP PROCEDURE sp_hexadecimal GO CREATE PROCEDURE sp_hexadecimal @binvalue varbinary(256), @hexvalue varchar (514) OUTPUT AS DECLARE @charvalue varchar (514) DECLARE @i int DECLARE @length int DECLARE @hexstring char(16) SELECT @charvalue = '0x' SELECT @i = 1 SELECT @length = DATALENGTH (@binvalue) SELECT @hexstring = '0123456789ABCDEF' WHILE (@i <= @length) BEGIN DECLARE @tempint int DECLARE @firstint int DECLARE @secondint int SELECT @tempint = CONVERT(int, SUBSTRING(@binvalue,@i,1)) SELECT @firstint = FLOOR(@tempint/16) SELECT @secondint = @tempint - (@firstint*16) SELECT @charvalue = @charvalue + SUBSTRING(@hexstring, @firstint+1, 1) + SUBSTRING(@hexstring, @secondint+1, 1) SELECT @i = @i + 1 END SELECT @hexvalue = @charvalue GO IF OBJECT_ID ('sp_help_revlogin') IS NOT NULL DROP PROCEDURE sp_help_revlogin GO CREATE PROCEDURE sp_help_revlogin @login_name sysname = NULL AS DECLARE @name sysname DECLARE @type varchar (1) DECLARE @hasaccess int DECLARE @denylogin int DECLARE @is_disabled int DECLARE @PWD_varbinary varbinary (256) DECLARE @PWD_string varchar (514) DECLARE @SID_varbinary varbinary (85) DECLARE @SID_string varchar (514) DECLARE @tmpstr varchar (1024) DECLARE @is_policy_checked varchar (3) DECLARE @is_expiration_checked varchar (3) DECLARE @defaultdb sysname IF (@login_name IS NULL) DECLARE login_curs CURSOR FOR SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM sys.server_principals p LEFT JOIN sys.syslogins l ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name <> 'sa' ELSE DECLARE login_curs CURSOR FOR SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM sys.server_principals p LEFT JOIN sys.syslogins l ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name = @login_name OPEN login_curs FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin IF (@@fetch_status = -1) BEGIN PRINT 'No login(s) found.' CLOSE login_curs DEALLOCATE login_curs RETURN -1 END SET @tmpstr = '/* sp_help_revlogin script ' PRINT @tmpstr SET @tmpstr = '** Generated ' + CONVERT (varchar, GETDATE()) + ' on ' + @@SERVERNAME + ' */' PRINT @tmpstr PRINT '' WHILE (@@fetch_status <> -1) BEGIN IF (@@fetch_status <> -2) BEGIN PRINT '' SET @tmpstr = '-- Login: ' + @name PRINT @tmpstr IF (@type IN ( 'G', 'U')) BEGIN -- NT authenticated account/group SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' FROM WINDOWS WITH DEFAULT_DATABASE = [' + @defaultdb + ']' END ELSE BEGIN -- SQL Server authentication -- obtain password and sid SET @PWD_varbinary = CAST( LOGINPROPERTY( @name, 'PasswordHash' ) AS varbinary (256) ) EXEC sp_hexadecimal @PWD_varbinary, @PWD_string OUT EXEC sp_hexadecimal @SID_varbinary,@SID_string OUT -- obtain password policy state SELECT @is_policy_checked = CASE is_policy_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name SELECT @is_expiration_checked = CASE is_expiration_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' WITH PASSWORD = ' + @PWD_string + ' HASHED, SID = ' + @SID_string + ', DEFAULT_DATABASE = [' + @defaultdb + ']' IF ( @is_policy_checked IS NOT NULL ) BEGIN SET @tmpstr = @tmpstr + ', CHECK_POLICY = ' + @is_policy_checked END IF ( @is_expiration_checked IS NOT NULL ) BEGIN SET @tmpstr = @tmpstr + ', CHECK_EXPIRATION = ' + @is_expiration_checked END END IF (@denylogin = 1) BEGIN -- login is denied access SET @tmpstr = @tmpstr + '; DENY CONNECT SQL TO ' + QUOTENAME( @name ) END ELSE IF (@hasaccess = 0) BEGIN -- login exists but does not have access SET @tmpstr = @tmpstr + '; REVOKE CONNECT SQL TO ' + QUOTENAME( @name ) END IF (@is_disabled = 1) BEGIN -- login is disabled SET @tmpstr = @tmpstr + '; ALTER LOGIN ' + QUOTENAME( @name ) + ' DISABLE' END PRINT @tmpstr END FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin END CLOSE login_curs DEALLOCATE login_curs RETURN 0 GO
2. Now run the stored procedure sp_help_revlogin to generate a script for each login. 3. Run the required login creation commands on the destination instance.

Tuesday, 23 October 2012

List all Objects per Filegroup

The following query can be used to list all obejcts per filegroup including the allocated data size.
SELECT ds.name AS DataSpaceName ,au.type_desc AS AllocationDesc ,au.total_pages / 128 AS TotalSizeMB ,au.used_pages / 128 AS UsedSizeMB ,au.data_pages / 128 AS DataSizeMB ,sch.name AS schemaName ,obj.type_desc AS ObjectType ,obj.name AS ObjectName ,idx.type_desc AS IndexType ,idx.name AS IndexName FROM sys.data_spaces AS ds INNER JOIN sys.allocation_units AS au ON ds.data_space_id = au.data_space_id INNER JOIN sys.partitions AS PA ON (au.type IN (1, 3) AND au.container_id = PA.hobt_id) OR (au.type = 2 AND au.container_id = PA.partition_id) INNER JOIN sys.objects AS obj ON PA.object_id = obj.object_id INNER JOIN sys.schemas AS sch ON obj.schema_id = sch.schema_id LEFT JOIN sys.indexes AS idx ON PA.object_id = IDX.object_id AND PA.index_id = idx.index_id WHERE obj.type_desc NOT IN('SYSTEM_TABLE', 'INTERNAL_TABLE') ORDER BY ds.name ,sch.name ,obj.name ,idx.name

Monday, 8 October 2012

SSIS - Overiding Package Variable Configuration Values on the Command Line

To overide SSIS pacakge variable values set in configuration files with those specified on the command line the following can be added to the command line.
/Set \Package.Variables[User::start_date].Properties[Value];2012-09-09

Powershell - Get a Specific Line from a Text File

To retrieve the specific line of a text file in powershell use the following command:
Get-content -Path myfile.txt | Select-Object -Index 60614
To retrieve the first 10 rows use:
Get-content -Path myfile.txt | Select-Object -First 10
To retrieve the last10 rows use:
Get-content -Path myfile.txt | Select-Object -Last 10

Thursday, 26 July 2012

Transaction Log Restore

The following statement can be used to restore a transcation log back up and keep the database on standby as read only.
RESTORE LOG [corporate_log] FROM DISK = N'\\10.0.1.83\dba\logship\dbname\filename.trn' WITH FILE = 1 ,STANDBY = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Backup\ROLLBACK_UNDO_dbname.BAK' ,NOUNLOAD ,STATS = 10