SQLLawman

Family man and SQL Geek

Restoring Last Full Backup

By MichaelDeputy, 9 days ago

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

By MichaelDeputy, 4 months and 15 days ago

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

Blog Site Changes

By MichaelDeputy, 5 months and 21 days ago

On a whim a few days ago, I looked at my feedburner statistics.  I was shocked to learn that there are 20 people who have subscribed  to my feed.  So I thought I better spend some more time on my blog in order thank those people for their time and attention.  So I am planning to have fewer of these useless posts and concentrate on more meaty posts that might actually help someone out.

Secondly, I decided that I wanted to change my twitter handle and I thought about it for quite a while and thought blending my last name with my job as a SQL Server DBA.  So I morphed into SQLLawman.  I'm following suit and changed my blog to follow suit.

Finally, I found a new theme for my blog and I really like the simple nature, so I think I'll work from here to edit and add features as time goes by.

Who’s Winning the Olympics?

By MichaelDeputy, 6 months and 12 days ago

I love the Olympics.  I enjoy watching the competition, and occasionally I enjoy the behind the scenes, human interest story.  But one thing that I struggle with is: Who is winning?  Depending win where you look you can see who has the most medals, who has the most gold medals, etc.  So I am going to throw my scoring into the mix.  Yes, it is probably as convoluted as the scoring in figure skating. (what is the difference between at toe loop and a lutz anyway?)  So here's my scoring:

  1. Every Gold is worth 3 points
  2. Every Silver is worth 2 points
  3. Every Bronze is worth 1 point

Yep, more points for the winners.  But this is where my scoring becomes interesting.  I divide the number of points by the number of athletes.  I found the number of athletes from http://www.vancouver2010.com/.  What I find cool about this is that figures out which country sent the best athletes.  That's my idea of who wins.  Since this my blog I get to make my own rules.  So without further ado, here's the current top 10: Final Standings Updated 2/28/10 2:55 pm EST

Place

Country

Athletes

Gold

Silver

Bronze

Points

Points per Athlete

1

Republic of Korea

46 6 6 2 32 .696
2

Netherlands

34 4 1 3 17 .500
3

Norway

99 9 8 6 49 .459

4

Germany

153 10 13 7 63 .412
5

Austria

81 4 6 6 30 .370
6

United States of America

215 9 15 13 70 .326
7

Canada

205 14 7 5 61 .298
8

Croatia

18 0 2 1 5 .278
9

People's Republic of China

90 5 2 4 23 .256

10

Poland

50

1

3

2

11

.220

Free Bacon at the PASS Summit

By MichaelDeputy, 1 year ago

Yes I'm serious.  If you are going to the PASS Summit in Seattle there is an opportunity to get FREE BACON.  The SQL Virtualization Virtual PASS chapter is hosting a breakfast and game show on Tuesday November 3rd at 7:30 am and you are invited.  Do you have questions about virtualizing  SQL?  Do you have answers about virtualizing SQL?  Do you not care about virtualizing SQL and just want a good breakfast?  Come join us.  Experts from VMWare will be on hand to answer questions.  There will be fun, prizes, and food.  Now here is where it gets really cool.  We are looking for contestants.  If you are thinking about virtualization, actively engaged in virtualizations or already been there, done that, bought the T-shirt  you may qualify to be in the game show.  Very soon we will have a registration page that will give you the opportunity of a lifetime to play.  So what kind of game will this be?  Have you seen Wipe Out?  A really fun game show about people embarrassing themselves on a muddy, messy, wet obstacle course with «Big Balls»?

s-WIPEOUT-large

Well, this has absolutely nothing to do with that, but it will be fun and energetic all the same.  If you are going to the PASS Summit click here for more information and to register for the event. If you want to ensure there is enough bacon for everyone, please register before the event.  Unfortunately you will not get to opportunity to meet me this  year at the summit, but I'm really hoping for next year.  So register, get some free stuff and bacon.

Find locked SQL accounts on SQL 2005

By MichaelDeputy, 1 year ago

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

By MichaelDeputy, 1 year ago

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

By MichaelDeputy, 1 year and 1 month ago

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.

Today's PSA

By MichaelDeputy, 1 year and 1 month ago

According to the American Red Cross here are 10 facts about the need for blood donation.

1. Every two seconds someone in the U.S. needs blood
2. More than 38,000 blood donations are needed every day
3. One out of every 10 people admitted in a hospital needs blood
4. Total blood transfusions in a given year: 14 million (2001)
5. The average red blood cell transfusion is approximately 3 pints
6. The blood type most often requested by hospitals is Type O
7. The blood used in an emergency is already on the shelves before the event occurs
8. Sickle cell disease affects more than 80,000 people in the U.S., 98% of whom are African American. Sickle cell patients can require frequent blood transfusions throughout their lives
9. More than 1 million new people are diagnosed with cancer each year. Many of them will need blood, sometimes daily, during their chemotherapy treatment.
10. A single car accident victim can require as many as 100 units of blood.

Please consider donating.  Whole blood can be donated every 56 days.

Stored Procedure with Execute as

By MichaelDeputy, 1 year and 3 months ago

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.

← Previous 01 02 03 04 Next →