Posts tagged SQL Server

The 3 most important words for a DBA

Occasionally I’m asked what the most important thing for a DBA to know is.  It seems some people want to know what 1 thing to focus on in order to be successful long term as a DBA.  I’ve pondered this question for quite a long time and I’ve given various answers over the years.

Reality intervenes

Ive come to the conclusion that there really is no one special thing or “skill” that its important for a DBA to know.  Sure, there are lots of qualities that tend to make a successful DBA.  Things like attention to detail, thirst for knowledge, and an uncanny love of BACON…  The problem is as huge as the SQL Server product is becoming it is also becoming increasingly difficult to be an expert in everything.  I am actually of the opinion that it is impossible to be an expert in everything SQL Server.

Knowing what you dont know

The most important thing for a DBA to know can be summed up in 3 simple words ” I Dont Know”  Why is that important?  Because, knowing what you don’t know, and being able to admit it to those around you is by far the most valuable skill that you can poses as a DBA at any skill level.

When in doubt its always good to remember that its ok to say “I don’t know” The Corollary to this statement is of course “Ill find out, or find someone who does know”.  Ive seen many small issues blow up into large problems over the years because someone didn’t really understand what was going on and they were afraid to let their coworkers (or boss) know that they don’t understand.  In many cases if we’ll just learn to say “I don’t know” some further pain can easily be avoided.

Database Automagic

This months TSQL Tuesday is hosted by a good friend Pat right over at  SQL Asylum

For this months entry I decided to keep it short and sweet, following in my Bits N Bytes theme.

The Meta Script

In the true sense of the word automation, this really doesn’t fit but, in the terms of quickly getting something done that would otherwise be a mundane repetitive task, this can save a world of time.

Lets say we have a list of objects in the Sales Schema and we have a request to grant Select and Insert access to a user for those objects.  There are two approaches, 1 is to grant select and insert to the actual schema like this

GRANT SELECT, INSERT ON SCHEMA::Sales TO BusinessUser 

However you might decide that you only want to grant direct SELECT and INSERT on the tables that exist in the DBO Schema today not those tables which may be created in the future (auditors love to make us do this)

A simple way to automate granting these rights is by writing a script that writes a script like so

SELECT
‘GRANT SELECT, INSERT ON ‘ + sch.name + ‘.’ + obj.name + ‘ TO BusinessUser’
   FROM sys.all_objects obj JOIN sys.schemas sch
 

    ON obj.schema_id = sch.schema_id

  WHERE sch.name = ‘Sales’
  and obj.type = ‘U’



This should give you a result set that looks something like the following:

GRANT SELECT, INSERT ON Sales.People TO BusinessUser
GRANT SELECT, INSERT ON Sales.Sales TO BusinessUser

At this point, run the output in a separate command window and viola you’ve automated that grant of permissions

This may not be true “automation” in the sense that Pat was looking for but, perfecting the ability to write scripts that write scripts is a huge timesaver

A tall tale of SQL database corruption

This corruption story begins like many.  Somebody in a server room far far away decided to make a change to a VMware guest machine and that little change rippled through our poor server like a lady Gaga Meat Dress through the VMA’s.  Needless to say, it wasnt pretty.  The full set of events may never be known by me but it appeared as though our guest server ran out of disk space on the OS and some form of recovery was done.Shattered into a million pieces

What we started with was a sql 2005 sp3 server where 1 of the drives was apparently corrupted, So 2 SQL instances wouldnt start.  They were both erroring with the message :

Error: 9003, Severity: 20, State: 1.
The log scan number (23:5736:37) passed to log scan in database ‘master’ is not valid. This error may indicate data corruption or that the log file (.ldf) does not match the data file (.mdf). If this error occurred during replication, re-create the publication. Otherwise, restore from backup if the problem results in a failure during startup.

Using trace flag 3608 and startup parameters -c -m I set about to do a normal “disaster” recovery of our server

After rebuilding the master database, everything came online successfully.  Then master was recovered from the previous backup.  Once master was online I started getting the very same error message about the model database

Error: 9003, Severity: 20, State: 1.

The LSN (11:999:1) passed to log scan in database ‘model’ is invalid

This would prove to be a trying error!  it took about several iterations and quite a time to figure out exactly what was going on.

On this server after initial setup we had moved the system databases from the install drive to seperate drives for log and data.  When rebuilding master, the system db’s wind up back in the default directories but, after recovering master, the databases are pointed back to the original locations.

Once we got the server started the log scan error message for model showed up so, I began what I thought would be a normal restore of the model database.  Unfortunately, there was no way for model to be restored.  During the restore command, I got alternating messages that the model database log file was corrupted


Error: 3283, Severity: 16, State: 1.

The file “modellog” failed to initialize correctly. Examine the error logs for more detail

The Error 3283 Would be followed by

the database ‘model’ is marked RESTORING and is in a state that does not allow recovery to be run.

After trying various iterations of deleting the existing model log & database files, copying in the newly created ones and running restores, nothing was working.  I began to think the disks were actually having problems, or the backup was bad.  After verifying both the backup and the disk config I was left with only a hail mary –> sp_detach_db

After detaching model, I copied in the newly created model files (from the rebuild of master) and ran sp_attach_db on them.  Once the Model database was attached the instance started successfully!

After the instance started model was restored from the same backup and the instance restarted.  Finally, once the instance came online, it was a standard restore of all the user databases.

Im not sure what about the logscan error in model caused the errors I saw, but, both instances behaved exactly the same.  I had to detach and reattach a blank model to make the other instance work as well.

After going through this, I went back and tried to reproduce the problems by intentionally corrupting model and its transaction log in various ways.  Every corruption I could cause in model behaved as I expected and a simple restore statement worked.  Im still not sure WHY this happened but, hopefully it wont happen again and if it does there wont be so much testing to figure out how to get model online

Changing the owner of nearly anything in SQL Server

The other day we were going through some old scripts and had a huge discussion about changing object ownership in SQL.  Forever the only answers were to recreate the objects or use the stored procedures: sp_changeobjectowner or sp_changedbowner.  Alternatively at times there were those of us who used some other undisclosed hack of system tables but, I’ve never done anything like that…


Since SQL 2005 Alter Authorization has been the right way to change ownership of nearly anything

This is the most common use of alter authorization but there are many more.

sp_changeobjectowner would look like this

sp_changeobjectowner 'Products.Description','AllenK'

While the updated syntax would look like this

ALTER AUTHORIZATION ON OBJECT::[Products].[Description] TO [AllenK]

What really brought this post about was because I was working with certificates recently and at the time I needed to change who the owner and I just exported/imported & recreated them.  It was easy and what I knew would work.  After looking through the syntax for alter authorization I realized I could & probably should have done it this way.  I guess old habits die hard.

ALTER AUTHORIZATION ON CERTIFICATE::[MYCERTIFICATE] TO [CERTOWNER]

Some out there might question why I’m rehashing syntax changes that happened nearly 5 years ago, The reality is that even in 2011 a lot of the installations of SQL Server I look at are still 2005 and many of the people I run into still use old ways of doing things, this is for you (and me)!

ALTER AUTHORIZATION ON OBJECT::[Products].[Description] TO [AllenK]

Something old is something new

For the new year I have decided to try something different with a series of small regular posts.

Dealing with our large team of developers and our DBA team as well, I often run across interesting discussions about small things that are easily overlooked.  Often times these discussions end in a exclamation of “I knew that!!”  Of course what really happend is the memory of that specific feature or tidbit has simply been paged out of our memory.  I intend to take these (typicaly) small tidbits of SQL goodness and publish them to hopefully jog your memory as well. 

It never hurts to have your memory paged back in occasionally and I hope this helps

Go to Top