Posts Tagged ‘Database’

Removing < CR >< LF > During a Query

Tuesday, March 22nd, 2011

As you may have noticed, or not noticed, I have not done anything blog-worthy in quite a while. Well, actually that is not true, I have done a few things in Windows PowerShell that I think are kinda cool but the blog post is still swimming around in my mind and I have not sat down at typed it out, yet. But anyway, I did something this morning that I bet will pop up again and I think I should put it somewhere what I can find it later. So you get to hear about it also. I was building a fairly simple SSIS package. Select data from a database and write it to a text file. They don’t get much easier in my experience. I was using < CR >< LF > as my new row indicator. That seemed to work and I was going on to my next project. The customer called me. It seems that embedded in two of the columns (a text field) the users could and did use < CR >< LF > to format the text in the field. Well that was really messing with my extract. Never fear, dear readers, there is a solution. My good friend Mr. Google (or maybe Mr. Bing) let me know that char(13) was a carriage return and char(10) was a line feed. Well that made it easy. I edited my select statement to look something like this:

SELECT dbo.table.column1
,dbo.table.column2
,replace(replace(dbo.table.column3,char(13),’’),char(10),’’) as ‘column3’
,replace(replace(dbo.table.column4,char(13),’’),char(10),’’) as ‘column4’
FROM dbo.table
ORDER BY dbo.table.column1

I no longer had < CR >< LF > inside the column impacting my extract. Everyone was happy.

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.

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.

Are All Test Environments Created Equal?

Tuesday, April 28th, 2009

I don’t know about your environment, but at mine we have two classes of systems: Prod and Test.  So as you can guess, anything that is not Prod is Test.  That’s a pretty simple definition.  Further defined anything that has a DR priority of 6 or less is PROD and anything 7 or greater (or not graded) is test.  This makes live very simple, or so it would seem.  So that brings me back to my question: Are All test environments created equal?  On the one hand an answer of “yes” lets everyone know what to expect.

  • Will the Test system be backed up?
  • If it is backed up, what is the retention?
  • What level of disk redundancy is available?
  • Can Test servers be physical hardware or VM only?
  • What is the recovery priority if needed?

This sounds like a pretty straight forward way to go.  But on the other hand does one size fit all? We use many of our test systems for training.  The current production system refreshed the test system, the upgrades are done to the test system and the end users are trained on the new system using the test environment.  New employees are also trained on this system on an ongoing basis.  So here’s the scenario.  A medical system is going live in 2 days.  100 nurses need to be trained. Training takes 2 hours.  The medical facility calls in PRN staff to cover the nurses during training.  Let’s just assume all the nurses make the nation average that I googled a few minutes ago of $45,000 per year + benefits or $21.63 per hour + 39% benefit package.  (yep, made that benefit number up makes it an easier $30.00 per hour labor cost) lets also say the training room can accommodate 10 students.  Just for fun, lets also say the contract with the trainer is for two 12 hour days at $50.00 per hour including expenses.  My calculations work out to about $660.00 per hour of training or sitting if the test system is not available. 

  • $30.00 per hour of the nurse in training
  • $30.00 per hour of the nurse replacing the nurse in training
  • $60.00 per instruction hour by the trainer ($600 per day/10 instruction hours)
  • Ten nurses and replacements

I’d hate to image the cost if we were training doctors rather than nurses.  Does that system have the same requirements as the “sandbox” I use to experiment and document the SQL 2005 SP3 upgrade?

So when does this discussion happen?  Usually it involves the SAN/Backup team.  They are charged with providing disk for servers and ensuring that what is on that disk is archived and recoverable within a reasonable time.  I don’t envy their position at all.  They also have to balance backup windows, and backing up test databases uses equipment and storage that the Prod system could be using.  There are only so many data paths and hours in the day and purging old data seems to be a thing of the past.  So I’m not sure a simplistic look at Prod/Test is appropriate.  As we look at tiered storage, tiered version of SQL do we also need to look at tiered test environments or break the model completely and add a couple more tiers.  Prod, Train, Test, Sandbox.  Then place policies and expectations at each level.  How about your environment?  Do you ever struggle with getting other teams to understand the cost of data that is not “Production”?  Of course that leads into the “we want to test this software and see if it solves the problem and now we have 100 users depending on it” issue, but that’s another day.

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.

Update: Degrees of Parallelism

Friday, February 13th, 2009

Well, it’s been 10 days since I started to play with parallelism on this server.  I have gotten the self blocking to stop.  My CPU utilization has stabalized and my phone is not ringing.  So I think that the issues is mitigated.  I finally settled on Max Degrees of Parallelism of 2 and the Cost Threshold for Parallelism of 500.  I also have the developers looking at the code to ensure all their joins are nolock, they tell me they should be, but I am asking nicely to them to verify.  So I’m moving on to other issues unless this one pops back up.

Starting up database ‘SomeDbName’

Wednesday, February 4th, 2009

I love it when I learn something within a half hour of getting to work.  I had an unrelated alert on a vendor supplied database server.  As I was investigating that problem I came across two things.  One was funny, one I actually learned something.  First the funny: they had named the sysadmin account ‘God’.  Wasn’t that from the terrifically awesome, how can I live without seeing that one, movie ‘Hackers?’  But now onto what I learned.  I looked at the SQL Server Log and I had hundreds of entries that said “Starting up database ‘DbName’” These were appearing several time a second.  I’m thinking.  This has got to be a waste of precious CPU.  I know the server site at 10% utilized even when the two people we bought it for are using it, but that’s beside the point.  Its CPU darn it.  Well, I have to admit that message meant nothing to me.  So I asked my favorite co-worker, Mr. Google, what was up.  He told me the server is set to Auto Close.  I looked at the options and he, of course, was right.  Auto shrink was enabled also.  So, I fixed those two issues and now the database server is chugging along at 9.98% utilized.  It’s only 7:44 am and I believe my work is done here.  Time to twitter for the next few hours till quitting time.  Crap, it only works that way in movies.  Another database needs my care.