Archive for the ‘MSSQL’ Category

My New Job

Wednesday, January 4th, 2012

Several months ago I accepted a promotion from my employer. This is my first foray into management. I have held onto my SQL responsibilities, but also took on the honor of supervising the Windows Server Team. This is a team of seven talented and capable individuals. One of the challenges to this position is that it came about due to a reorganization and two disparate teams have been brought together to be one server team. Three of the team members work with me at the data center, and four were dispersed across the other hospitals in my organization. Turning these hard working, talented individuals into a cohesive team is my initial goal. Doing this as we open our second data center is adding some to that challenge.
One of my first responsibilities was to hire the eighth member of my team who will at least for a while fill a unique position as a facility and data center based employee. The gentleman I chose brought a fantastic tool set to the team that I will be utilizing in the future. He is also taking his first steps to become a SQL DBA and free up more of my time to lead.
I wish I could say that I have all the tools and knowledge to make this a smooth transition for the team and myself, but instead I am learning how much I need to learn about personalities, and more importantly how to lead my team to fulfill my vision.

But that is all for now. More as I continue to figure out where I am going.

Restoring Last Full Backup

Tuesday, August 31st, 2010

I ran into a small problem where I needed to automate a restore of my last full backup to a different database.  Since my full backup file names include a date/time stamp I needed to query msdb for the backup name.  Since I figured it out, I thought I’d post it so I would know where of find it the next time I need it, and maybe help someone else out in the process.

declare @bksource varchar(100)
select @bksource=(
select a.physical_device_name
from [msdb].[dbo].[backupmediafamily] a join [msdb].[dbo].[backupset] b on b.media_set_id = a.media_set_id
where b.backup_start_date =
(
  select max( backup_start_date ) from [msdb].[dbo].[backupset]
   where database_name = b.database_name and type = ‘D’
)
and database_name = ‘<database_name>’
)

– print @bksource

restore database [<Database_Name2>] from disk = @bksource with replace, stats = 10,
MOVE ‘<Database_Name>’ TO ‘i:\mssql\data\test\<Database_Name2>.MDF’,
MOVE ‘<Database_Name>_log’ TO ‘j:\mssql\xlog\test\<Database_Name2>_log.LDF’
GO

Replace anything inside the <> with the proper names and set the correct drive letters.

DBCC Checkdb

Sunday, April 25th, 2010

Last weekend I attended SQL Saturday in Chicago.

How do I get all my SQL server healthy?

How do I get all my SQL server healthy?

During one of the sessions there was a brief discussion about DBCC CHECKDB.  The long and short of it:  It is important, do them, it is not an option to skip this check.  It is right up there with Index maintenance and updating statistics.  But, no one ever said that you had to run them on your production database.  I repeat, no one said you had to run them on your production database.  Here’s a couple of thoughts.  First when you do your weekly backup/restore test, notice I didn’t say ‘if’, run the DBCC CHECKDB against your restored copy.  If you have inconsistencies in your production database, it will also exist in an exact copy of you database.  But remember to fix it on your production database and not you copy that you will drop later.  Another option is if you use backup/restore to create a report server.  This is an excellent way to do the checks and verify your backups are good.  We have a couple of systems that we do this every night when the SQL backup is complete, we restore it the report server.  Proved the backups are good.  I then look for a time that Crystal Info is not pounding the report server and run the dbcc checks against that database copy.  I have it e-mail me if it finds any errors.  Kills two birds with one stone.  Now for the truth in my shop.  With over 120 instances and over 1600 databases, a weekly backup test and DBCC CHECKDB is just not practical.  So I use a hybrid approach.  The servers that refresh a report server daily are easy perfect candidates, our larger (high priority) databases get a restore to a test server and DBCC CHECKDB there, and the other databases get an occasional backup/restore test and DBCC CHECKDB run against the production database once per week during slow times.  So think outside the box.  Find a way to verify your database’s consistancy.

Dep

Find locked SQL accounts on SQL 2005

Wednesday, September 16th, 2009

We are in the process of upgrading a Microsoft product that just hasn’t gotten around to integrating with AD and uses SQL accounts.  It also has a really bad habit of locking SQL accounts if your AD policy for failed login attempts is set low.  So I got tired of searching through SSMS to unlock accounts, so I wrote a quick script to list the locked accounts and the command to unlock it.  I didn’t go as far as to auto-unlock the locked accounts but that would be a simple edit.  Here’s the script.

Declare @UserID varchar(25), @status nvarchar(5), @Command varchar(255), @command1 varchar(255)
Declare Account_cursor Cursor
for select name from sys.syslogins where name not like ‘<DOMAIN>\%’
open Account_cursor
FETCH NEXT FROM Account_cursor
INTO @UserID
WHILE @@FETCH_STATUS = 0
BEGIN
select @status= convert(nvarchar,(loginproperty(@userID,N’IsLocked’)))
if @status = ’1′
BEGIN
print ‘– ‘ + @userID + ‘ is locked’
select @command = ‘ALTER LOGIN [' + @userID + '] WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF, NO CREDENTIAL’
select @command1 = ‘ALTER LOGIN [' + @userID + '] WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=ON, NO CREDENTIAL’
print @command
print @command1
– exec (@command)
– exec (@command1)
END
fetch next from Account_Cursor into @userID
END
Close Account_cursor
Deallocate Account_cursor

Thank you @unclebiguns for pointing me toward the function that makes this all possible.

I am guessing that this is not the easiest or most efficient way to do this, so if anyone would like to educate me on a better way, I am very open to learning your techniques.

Find all Triggers

Tuesday, August 18th, 2009

I was asked to find all tables in a database that had a DML trigger so a developer could ensure that all the old functionality was available in the new database.  So once I built my query to give me the information, I thought I’d save it here for future use.

select a.name as ‘Trigger’, b.name as ‘Table’
from sysobjects a
join sysobjects b
on a.parent_obj=b.id
where a.xtype = ‘TR’

This gave me a nice little result set of each trigger and what table it was associated.  Maybe I won’t have to re-invent the wheel the next time they ask.

SKU is Invalid when installing 2nd node in SQL 2008

Thursday, July 16th, 2009

I was installing SQL Server 2008 on a Windows 2008 cluster the other day and I received and error:  ‘The current SKU is Invalid’.  So I went looking for the resolution.  I came across  a blog post by Vittorio Pavesi that pointed me to the answer.  I did not take the course of copying the install files to the local server.  I was installing from a network share, so I simply moved the file  DefaultSetup.ini to a new location and started the ‘Add node to a SQL Server failover cluster’  again and entered the key included on the DefaultSetup.ini manually.  The rest of the install went without issue.

Stored Procedure with Execute as

Tuesday, June 2nd, 2009

We have a database that holds information that is fairly personal, so much that the application team that supports it may only have read access to the database.  There came a need to occasionally lock all the administrators out of the system for updates.  These updates will occur every few months.  So I created a SQL user and gave that user select and update rights to the necessary table.  Then I created a couple of stored procs, one to disable the logins and one to enable the logins.

CREATE PROCEDURE [dbo].[LockOutAdministrators]
WITH EXECUTE AS ‘EXECUTIONER’
AS
BEGIN
BLAH, BLAH, BLAH
END

I then granted execute to the stored procedures to the application group.  So the application team can only update the table via the stored procedure that does only what it needs to do.  They are happy they have an easy way to do this without me becoming the bottle neck.  If this was a more sensitive system, I’d probably add some auditing, but it is not necessary at this time.

Renaming a SQL server

Monday, April 13th, 2009

Here’s a little simple one that I’ll probably need again, so I thought I’d just blog it:

How to rename a SQL server used mostly when the windows server it resides on changes names:

To change the name of a default instance of SQL, run the following procedures:

sp_dropserver <old_name>
GO
sp_addserver <new_name>, local
GO

To change the name of a named instance of SQL, run the following procedures:

sp_dropserver <old_name\instancename>
GO
sp_addserver <new_name\instancename>, local
GO

Cleaning up the disk

Monday, April 6th, 2009

When I became our companies MSSQL DBA I inherited an smooth running machine.  My predecessors were extremely talented and their scripts did everything we needed done.  But I can’t just live off their hard work, I need to leave my mark on the environment.  So I am tweaking some of their scripts to get rid of what annoys me.  The current task is old backup files.  The scripts they wrote do this awesome thing that archives a backup if the archive bit has not been cleared.  This is so cool.  If NetBackup has a problem, the old backup file that hasn’t gone to tape/disk is moved to an archive folder and then the new backup file is created.  But what happens when after the file is backed up and the archive bit is cleared.  It just sits on the disk and is backed up again with every full backup that occurs.  That makes the backups bigger and take longer.  So I added a job that deletes all *.bak files that have their archive bit cleared.  That was so much easier than I expected it to be.

exec xp_cmdshell ‘del d:\mssql\backup\*.bak /A:-A /S /F’

I run this command 15 minutes before I do the nightly backup.  It frees up space so I get less alerts of full volumes.  It decreases the NetBackUp full backup time and more backups can happen within their windows.  Unfortunately the DBAs before me decided that transaction log backups and database backups should have an extension of .bak.  Since I want to treat transaction logs differently I have to go back and change the backup extension.  So I’m slowly working through the databases to make that change.  So once that is done, I can start cleaning up my backup volumes.

Find the Windows Server Name

Thursday, February 19th, 2009

I was working with SQL2000 on a windows cluster.  I needed to write the results of a query to a test file on the local drive.  I was using @@servername to get the server name to write the file.  Unfortunately that appends the instance name and gave me a problem.  So I went looking for how to remove the instance name and I found a pretty neat trick.

I ran the command: select @@servername and received ‘server\instance’

I ran the command: select @@servicename and received ‘instance’

So I thought there must be a way to subtract @@servicename from @@server name, then I tried this:

select replace(@@servername,’\’ + @@servicename,”)

My result was: ‘server’

That I can work with.