SQL Server (RSS)

MS SQL Server 2000-related posts

Quick Tip: Retrieving Report Definitions from the Catalog Table in the ReportServer Database

The Reporting Services Catalog table in the ReportServer database contains the RDL XML in the Content column, serialised to binary format.

I had an issue recently where I needed to retrieve the RDL XML from the Content column from a backup of the ReportServer database using SQL Server 2005. Here's the code that did the job:

SELECT  [Name], 
        CAST(CAST([Content] AS VARBINARY(MAX)) AS XML) AS reportXML 
FROM    ReportServer_Backup.dbo.[Catalog] WITH (NOLOCK) 
WHERE   --get only reports, not folders or other resources 
        [Type] = 2 

I would not recommend running this against the entire table. I've used the NOLOCK hint to avoid locking up the table, and also screened our non-reports by adding Type = 2 to the where clause.

I originally found this great little snippet by at the SQL Server and XML blog. Thanks Jacob! It came in so useful that I thought I'd write it down for later.

Tags: sql server, reporting services, database, xml

Book Review: Inside Microsoft SQL Server: 2005 T-SQL Querying, by Itzik Ben-Gan, Lubor Kollar, and Dejan Sarka

The first thing I need to say about this book is: wow.

T-SQL Querying is easy to read and yet really gets in depth with T-SQL. It reminds me of Itzik's presentations - complex concepts explained in a comfortable and informative style. The book covers query processing and optimisation, has some useful scripts for performance monitoring, logic puzzles, the new features of SQL Server 2005 like CTEs, ranking functions and APPLY, and plenty more. Some sections of the book could easily become my standard reference material on logical and physical query processing, while other sections are almost tutorial-like.

One of the advantages of reading this 600+ page book on paper over blog posts, is that a whole chapter can be dedicated to explaining a concept using increasingly complex examples. Another advantage is that most sections of code are followed by the corresponding results, which are in turn often followed by query plan results, so you can see the input, output and plan without necessarily having to run the query. In a blog post, this would just take up too much room.

I've already been able to put some of Itzik's hints and tips to good use, as well as finally using CTEs and understanding the "new" DMVs (things I knew about before, but didn't quite "get").

This book gets 2 thumbs up, 5 stars and 10 out of 10 from me. I guess next on my reading list will be the companion book Inside Microsoft SQL Server 2005: T-SQL Programming.

Tags: sql server, review, Itzik Ben-Gan

A Cheap, DIY way to document SQL Server databases

I've always had a problem with documentation. I agree it's absolutely necessary; it's just often the requirements for documentation are so vague that I never know if I'm done or not.

One way to enforce standards when documenting tables and views in SQL Server is by using extended properties and auto-generating the documentation. I've used many free tools to do exactly this over the years; currently I favour SQLSpec.

If you're interested in a cheap, do-it-yourself method of documenting SQL Server databases, check out this article titled "Create a SQL Server Data Dictionary in Seconds using Extended Properties" at MSSQLTips. Although I haven't tried it, it looks like a helpful article if you're into customising the output of your documentation.

Tags: sql server, documentation, extended properties

Adding Object Permissions to SQL Server 2005 Script As...Create Right Click Menu

One annoyance I have with SQL Server 2005's right click "Script As...Create" is that permissions on the object(s) to be scripted are not included by default.

You can change this in SSMS by going to Tools, Options and then selecting "Scripting" from the tree. There are lots of options for changing how scripts are generated - see this MSDN entry for the full rundown on scripting options.

To add permissions to the "Script As...Create", scroll down to "Object scripting properties", and set "Script permissions" to "True". Done!

Now if only I could get a DROP in the script before the create...

Tags: sql server, script, management studio

What are SQL Server Agent Job 'Categories'?

What does the "Category" drop-down for SQL Server Agent jobs do? The options are so limited and make no sense to me: "Full-Text"? "REPL-Alert Response"? "Web Assistant"?

If, like me, you've ever asked yourself the above question, wonder no more. Tim Ford has an article on MSSQLTips.com titled "Custom job categories to organize your SQL Agent jobs" which answers this, and more.

Tim's article is so useful and simple that immediately after reading it, I went out and added a couple of custom categories, and then assigned my "[Uncategorized (Local)]" jobs. All this took less than 10 minutes, and now I know about SQL Server Agent Job categories. Thanks Tim!

Tags: sql server, sql agent, Tim Ford

Microsoft Course 2780 Maintaining a Microsoft SQL Server 2005 Database at Solid Quality Learning

Recently, my boss paid for me to attend Microsoft Course 2780 Maintaining a Microsoft SQL Server 2005 Database at Solid Quality Learning (thanks, boss!)

Around 18 months ago, our department was put in charge of a SQL Server 2005 box. I've been using SQL 2005 since then which of course gave me a huge headstart in the course. Way back when, I attended the 2779 training (focused more on developers), and thought that the 2780 training would complement it and make me into a DBA :-)

Well, the training is over, and I'm not a DBA, but I got a lot out of the course; below is my quick review on the good, the bad, and the ugly:

The Good

  • I would recommend this course if you are trying to come to grips with the DBA side of things in SQL Server 2005.
  • Rob Farley did a fantastic job standing up front of the class. He was knowledgeable and friendly, happy to answer questions and go "above and beyond" the standard course material.
  • I got to talk to a bunch of smart guys, like Greg Linwood, Brett Clarke and Simon Gerada from Solid Quality Learning.
  • The other attendees were the most polite IT professionals I've ever met. Everyone got along, and the class atmosphere was relaxed.
  • There was real food from a real cafe.
  • Our machines were P4 2.8GHz with 3.5 GB of RAM, running Windows XP, and all course work was done in pre-configured virtual machines. I had no technical hitches (Vista would have been nice, though).
  • Rob mentioned that originally the course was too short at just 3 days. 5 days was about right for the 9 modules covered.

The Bad

  • Um, not much here. Possibly too much typing of SQL as opposed to using the GUI :-)

The Ugly

  • The price - I realise we're actually paying for having an expert tutor and not the sum of all the good things like the PC's, lunch, and course book...but why is IT training so darned expensive?
  • Documentation - The standard MS coursebook is overly verbose, and does not feature a single screenshot. You could never sell a book like that.
  • Only instant coffee (unless you went to the cafe downstairs) :-(

All in all, this was a great training that a got a lot out of. Rob added real-world hints & tips, and it was also interesting to hear the other participants tales of managing multiple (several hundred, in one case) servers and different versions of SQL Server.

Tags: sql server, database, training, Solid Quality Learning, Rob Farley

2 Code Snippets from SQL Server Training In Melbourne

I'm very fortunate to be at SQL Server 2005 training in Melbourne all week this week. Our instructor is Aussie SQL Server MVP Rob Farley who blogs at msmvps.com/blogs/robfarley/ and is unbelievably knowledgeable about the product (of course).

So far I've collected 2 useful code snippets from Rob - more on useful stuff from the course later:

--show all plans in the cache, courtesy Rob Farley
--adding "OPTION (RECOMPILE)" means that subsequent executions
--of this query aren't added to the cache
SELECT  *
FROM    sys.dm_exec_cached_plans p CROSS APPLY
        sys.dm_exec_sql_text(p.plan_handle) t CROSS APPLY
        sys.dm_exec_query_plan(p.plan_handle) q
OPTION (RECOMPILE)

--comma-separated from resultset in 1 call, courtesy of Rob Farley
--here we'll return all database names in a comma-separated string
--the trick is to give no column name (in the inner select) and no
--argument to XML PATH
--STUFF replaces the first comma with an empty string
SELECT STUFF((
SELECT ',' + name
FROM sys.databases
ORDER BY name
FOR XML PATH('')
), 1, 1, '')

Enjoy!

Tags: sql server, database, training, snippet

Still on SQL Server 2000 with SP4?

If so, then it's worth knowing that mainstream support ends April 2008 (via Andy Leonard, who asks "What's that ticking sound?")

Tags: sql server, support, microsoft

Reporting Services Matrix Techniques

I haven't posted anything on Reporting Services recently.

So, I have two links for the infamous matrix control:

Because I feel I need to compensate :-)

Tags: sql server, database, reporting services, matrix

Kill All Processes for a Passed Database Name (SQL 2005)

We have an upcoming requirement to kick all users out of a database before running a backup.

So I'm saving this script for future reference (thanks to Chris for posting it to the SQL Down Under List):

CREATE PROC [dbo].[sp_SpidKill]
  @db VarChar(200)
AS

DECLARE @Tmp VarChar(10)
DECLARE @spid VarChar(10)
DECLARE @Kill VarChar(200)

SELECT @spid = Min(spid)
FROM master.sys.sysprocesses
WHERE dbid = DB_ID(@db)

WHILE @spid IS NOT NULL
BEGIN
  SET @Kill = 'KILL ' + @spid 
 EXEC(@Kill)

  SET @Tmp = @spid
  SET @spid = NULL

  SELECT @spid = Min(spid)
  FROM master.sys.sysprocesses
  WHERE dbid = DB_ID(@db)
   AND spid > @Tmp
END

Tags: sql server, database, development, backup, script

Reporting Audit Changes to SQL Server 2005 objects

Following on from my last post on "Auditing Schema Changes to SQL Server 2005 objects", here's a simple bit of code that will take the EVENTDATA XML and transform it back into rows (note you need to have already run Richard's trigger and table creation script):

--EventData XML is element-centric. Below is an example, as elements will vary
--depending on what the "event" actually is:
/*
<EVENT_INSTANCE>
  <EventType>ALTER_TABLE</EventType>
  <PostTime>2007-10-02T15:39:42.707</PostTime>
  <SPID>71</SPID>
  <ServerName>SERVER</ServerName>
  <LoginName>DOMAIN\USERNAME</LoginName>
  <UserName>dbo</UserName>
  <DatabaseName>TestDatabase</DatabaseName>
  <SchemaName>dbo</SchemaName>
  <ObjectName>tblTest</ObjectName>
  <ObjectType>TABLE</ObjectType>
  <TSQLCommand>
    <SetOptions ANSI_NULLS="ON" ANSI_NULL_DEFAULT="ON" ANSI_PADDING="ON"
     QUOTED_IDENTIFIER="ON" ENCRYPTED="FALSE" />
      <CommandText>
        ALTER TABLE dbo.tblTest DROP CONSTRAINT DF_tblTest_Test
      </CommandText>
  </TSQLCommand>
</EVENT_INSTANCE>
*/
--just get back the necessary elements from the audit table - could also include
--SPID, server name, grantor, permissions, grantee, etc. depending on "event"
SELECT  --most of the elements are cast as SYSNAME...difficult to find the *real* datatypes
        O.[EventData].value('(/EVENT_INSTANCE/EventType) [1]', 'SYSNAME') AS [EventType],
        O.[EventData].value('(/EVENT_INSTANCE/PostTime) [1]', 'DATETIME') AS [PostTime],
        O.[EventData].value('(/EVENT_INSTANCE/LoginName) [1]', 'SYSNAME') AS [LoginName],
        O.[EventData].value('(/EVENT_INSTANCE/ObjectType) [1]', 'SYSNAME') AS [ObjectType],
        --put together full object name from database, schema and object
        O.[EventData].value('(/EVENT_INSTANCE/DatabaseName) [1]', 'SYSNAME') + '.' +
            O.[EventData].value('(/EVENT_INSTANCE/SchemaName) [1]', 'SYSNAME') + '.' +
            O.[EventData].value('(/EVENT_INSTANCE/ObjectName) [1]', 'SYSNAME') AS [FullObjectName],
        --actual command text (may be very long)
        O.[EventData].value('(/EVENT_INSTANCE/TSQLCommand/CommandText) [1]', 'NVARCHAR(MAX)') AS [CommandText]
FROM    --using Richard's example, get data from the "Audit.Objects" table, with XML column
        Audit.Objects O WITH (NOLOCK)

My standard disclaimer is "it worked on my machine". Your mileage may vary.

The output from this query is a normal rowset and can be used in whatever reporting tool you favor (e.g. for me, Reporting Services).

p.s. Thanks to Whitney for the pointer on XML data types and queries!

Tags: sql server, audit, schema