Wednesday, July 9, 2014

Setting Up a New SQL Server 2008 R2 Cluster - Make SURE that the Cluster Object has Rights in Active Directory!

  Being a DBA is something akin to being a duck-billed platypus: people try to classify you, but you really end up fitting into multiple categories. A DBA ends up doing lots of different things: writing SQL scripts, setting up jobs, restoring backups, and even some server administration.

  At work, we normally have the vendor set up things like SQL Server clusters and we manage them after they are set up and running. For reasons unknown to me at the time, we had a vendor try and fail 3 times to set up a SQL Server cluster for us. The job then fell on me. I was a little apprehensive at first because LUNs, SAN space, etc are foreign concepts to me, but I said I would give it a try. I got really lucky. In his previous position, our network engineer was a server guy and had installed a SQL Server cluster from scratch - so I had some great wisdom and input from him. I was able to Google a great deal of what to do and, of course, I had our network engineer in the cube in front of me. Honestly, everything went seamlessly until I would get to the end of installing the first instance. It failed over and over again and we could not figure out why.

  This may be something of a perfect storm because it involves Active Directory and AD here at my workplace may run differently than AD at your workplace, but I learned several things in this process. One is that when we are creating a SQL Server cluster, the cluster itself is an object in AD. What we finally found out was happening was that our newly created SQL cluster object needed to create other objects in our domain to complete the install and configuration, but it did not have the rights to do so in AD. You can be logged in as Domain Admin all day long and it will not make a difference unless the cluster object that you have created has the appropriate level of rights in AD. This one bit me good (and several times!) so I am making sure that I make note of it and underline it =)

Friday, October 4, 2013

Reboot Windows 2000, XP, and Vista from a Command Prompt

I came across a situation today where I needed to reboot an ancient XP workstation. However, since I was remoted into it, it did not give me that option in the start menu (at least, I am guessing that is why I did not see the option). At any rate, I was able to Google this and it was a great solution. Open up a command prompt. Type:

SHUTDOWN -r -t 01

Then press Enter. In a dozen years of working in technology, I never had occasion to do this. The great thing about technology is that I am always learning new things =)

Monday, August 26, 2013

Querying the Default Trace File

  It has always been a bit of a pain having to open up a SQL Server trace file with SQL Server Profiler and try to find the information I am seeking. It's true that the trace can be saved to a table and queried, but this method is SO EASY. It is also great for auditing if you do not have SQL Server 2008 R2 Enterprise installed (We run SQL Server 2008 R2 Standard here at work). 

  There are several ways to run this query. Here is one that I am currently using in production to track any configuration changes:

DECLARE @TRC_PATH VARCHAR(500)

SELECT @TRC_PATH = CONVERT(VARCHAR(500), value) 
FROM fn_trace_getinfo(DEFAULT)
WHERE property = 2 

SELECT TEXTData, HostName, ApplicationName, DatabaseName, LoginName, SPID, StartTime
  , EventSequence
FROM fn_trace_gettable(@TRC_PATH,1) fn
WHERE TEXTData LIKE '%configure%'
AND SPID<>@@spid
ORDER BY StartTime DESC

  Of course you can change things around to suit your own needs - different fields in the SELECT, change the conditions of the WHERE clause, etc.

FYI, I got this info from this great, great article available at mssqltips.com: http://www.mssqltips.com/sqlservertip/2364/capturing-and-alerting-on-sql-server-configuration-changes/

Wednesday, August 14, 2013

Disable All Enabled Jobs and Re-enable Them

  I recently came across a situation where I was working with a vendor that was upgrading one of our applications. He asked me to backup the production database and disable all jobs on the production SQL Server. I could just go through the SSMS GUI and do all of this, but I like to automate anything I can. It keeps me on my toes and (most of the time) makes it to where I don't have to be remoted in or at my desk if something needs to happen during off hours. 

  Simply disabling all of the jobs should be pretty easy, right? Jobs in SQL Server are stored in the msdb database in the sysjobs table. Since I want to disable all the jobs, I need to change the [enabled] tinyint field in the sysjobs table and change it to 0 (0 for disable, 1 for enable). Easy peasy:

USE [msdb]

UPDATE sysjobs
SET [enabled] = 0

However, there is one small problem with this. I this particular situation, there are 15 jobs on this SQL Server and only 13 of them are enable. There are 2 disabled jobs on this server, but we do use those jobs from time to time, so I don't want to delete them. The jobs will need to be re-enabled when the vendor finishes the work, but if I simply run:

USE [msdb]

UPDATE sysjobs
SET [enabled] = 1

that enables ALL of the jobs, including the 2 that need to stay disabled. So here is how I got around it. I decided to create a table and store the job_id (the unique identifier for the job in the sysjobs table) for each enabled job in it. I can use our good friend, the SELECT INTO statement, to create this table on the fly:

USE [msdb]

SELECT job_id
INTO enabled_jobs
FROM sysjobs
WHERE [enabled] = 1

So now I have a snapshot of the enabled jobs at a given point in time. I can then run an UPDATE statement and disable all of these jobs for the vendor:

USE [msdb]

UPDATE sysjobs
SET [enabled] = 0
WHERE job_id IN
  (SELECT job_id
  FROM enabled_jobs)

But there is just one more little problem: I want to automate this. Since I need to take my snapshot of which jobs are enabled at a point in time, I may need to create and recreate the enabled_jobs table over and over - especially if this code is to be reusable. So let's try this:

USE [msdb]

-- DROP TABLE if it already exists
IF OBJECT_ID (N'dbo.enabled_jobs') IS NOT NULL
  DROP TABLE enabled_jobs

-- Get the job_id of all jobs that are currently enabled and create a new table
-- on the fly to hold those IDs
-- Store these job_ids in a table so that there is a record of the jobs that were 
-- enabled and the ones that were not
SELECT job_id
INTO enabled_jobs
FROM sysjobs
WHERE [enabled] = 1

-- Disable all jobs that are enabled
UPDATE sysjobs
SET [enabled] = 0
WHERE job_id IN
  (SELECT job_id
  FROM enabled_jobs)

Now I can put all of this in a stored procedure and reuse it. Since we do not drop the enabled_jobs table at the end of our statement, it still exists and I can use it later to re-enable these jobs when our vendor has completed the work. It will re-enabled the ones that were previously enabled and keep the ones that were disabled disabled:

USE [msdb]

UPDATE sysjobs
SET [enabled] = 1
WHERE job_id IN
  (SELECT job_id
  FROM enabled_jobs)

I hope you enjoyed this entry and that maybe it even saved you a little time or that you learned something.

Wednesday, July 31, 2013

Changing the Restrict Access Option in SQL Server

Ever get something like this annoying popup when you try to change a SQL Server database from SINGLE_USER to MULTI_USER?


This is what always happens to me when a database is already locked down into SINGLE_USER mode and I try to change the property in the GUI. In order to change it, I run this T-SQL in SSMS (SQL Server Management Studio):

ALTER DATABASE [MyDatabase]
SET MULTI_USER WITH ROLLBACK IMMEDIATE;

If there is already an operation going on in the database, the "WITH ROLLBACK IMMEDIATE" will take things back to their previous state before the transaction started. After the statement completes, more than one authorized user should be able to access the database. Always use caution when performing any ALTER DATABASE statement on a production server. When a database is locked down in SINGLE_USER mode, an icon of a person will appear next to the database in SSMS:


If you refresh your view of the databases in object explorer after running the above T-SQL, the person icon should disappear from the database indication that it is in MULTI_USER mode.

Wednesday, November 14, 2012

The DATALENGTH() Function

Yesterday I was getting a very interesting error in SSRS. A user was trying to export data to Excel and it was basically telling her that it was too much data to fit into Excel. I began to suspect that one of the fields in the report was quite large. I went to run a standard T-SQL query to see exactly how large the field in question was. The field was named [Description] and it's type was text:

USE [MyDatabase]

SELECT LEN([Description])
FROM MyTable

Of course, you know what happened:

Msg 8116, Level 16, State 1, Line 3
Argument data type text is invalid for argument 1 of len function.

So, the LEN() function cannot be used on text type fields. Was there a LEN() equivalent for the text type? A little googling revealed that there was:

DATALENGTH()

This function was exactly what I needed to get the length for a text type field.



Monday, October 1, 2012

Finding the Name of the Primary Domain Controller

1. Open a command prompt
2. Type echo %logonserver%

That's it - not very complicated at all. The output in the command prompt is that machine's PDC (Primary Domain Controller).