Archive for June, 2009

SQL SERVER – Difference between Line Feed (\n) and Carriage Return (\r) – T-SQL New Line Char

Tuesday, June 30th, 2009

Today, we will examine something very simple and very generic that can apply to hordes of programming languages. Let’s take a common question that is frequently discussed – What is difference between Line Feed (\n) and Carriage Return (\r)?

Prior to continuing with this article let us first look into few synonyms for LF and CR.

Line Feed – LF – \n – 0×0a – 10 (decimal)

Carriage Return – CR – \r – 0×0D – 13 (decimal)

Now that we have understood that we have two different options to get new line, the question that arises is – why is it so?

The reason is simple. Different operating systems have a different way of understanding new line. Mac only understands ‘\r’ as new line, while Unix and Linux understand ‘\n’ as new line character. Our favorite OS windows needs both the characters together to interpret as new line, which is ‘\r\n’. This is the reason why a file created in one OS does not open properly in another OS and makes it messy.

Now, let us see how we can create a new line in SQL Server. It is a very simple script yet very useful when we have to do run print something or generate scripts. I have illustrated two examples below that are very easy to understand. In the first example, there are no new line chars inserted and for the same, everything is displayed in a single line. However, in the second example, new line char is inserted and the lines are separated with a new line.

Example 1: No new line feed char

DECLARE @NewLineChar AS CHAR(2) = CHAR(13) + CHAR(10)
PRINT ('SELECT FirstLine AS FL SELECT SecondLine AS SL' )
GO


Example 2: With new line feed char

DECLARE @NewLineChar AS CHAR(2) = CHAR(13) + CHAR(10)
PRINT ('SELECT FirstLine AS FL ' +@NewLineChar + 'SELECT SecondLine AS SL' )
GO

I hope my examples make things more clear to you all. Let me have your feedback on this article.

Reference : Pinal Dave (http://blog.SQLAuthority.com)

Posted in Pinal Dave, SQL, SQL Authority, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQL Utility, SQLServer, T SQL, Technology

When Did That File Get So Big?

Tuesday, June 30th, 2009

omg1 I haven’t blogged much since getting back from vacation.  It seems like a good restart after the two week hiatus would be to address an issue that has arisen countless times.  And while addressing it, I’ll put it into the DBADiagnostics database that I’ve blogged about a few times before.

Sudden File Growth

In a number of SQL Server environments that I’ve worked in there either isn’t a method for monitoring file size or the process is more of a rubber stamp morning check.  The DBA gets in and looks at the size of the files and if there isn’t a log of changes to update nothing is really done and changes aren’t investigated.

In most cases this won’t be a problem.  For instance, if you’ve gone out and pre-grown your data and log files to appropriate sizes then nothing will grow and there is nothing to see here.

In cases were the unexpected happens, though, database files can and will grow.  And in the worst of these cases, which only occur at night or on vacations, the files will grow to a point where there is no longer any disk space available.  And if your annual review is next week, this will happen to the log file and force your database offline.

Knowing Is Half the Battle

In these types of situations, I like to recall the last couple minutes of the GI Joe epos ides that I watched as a kid.  They almost always ended with the quote, “Knowing is half the battle!  Yo, Joe!”.  If I know that a file growth has happened then I can do something about it.

Now the best case is to know, monitor, and plan for upcoming file growths.  This is what might be called a best practice and if you’re not doing it I’d really recommend putting a process in place.  But we need to be prepared for the unexpected.  And even in the most best, rock solid environment, I’d recommend a file growth monitoring process.

The Solution

This procedure for the DBADiagnostics database differs slightly from previous alerts.  Instead of sending out an e-mail that aggregates all of the changes an error is raised for each file change in question.  This method was selected because this allows the process to be tool agnostic.

If the client has a log file monitoring process, then the error can be picked up that way.  Or if all of the alerts are generated from the SQL Server instances, then a SQL Agent Alert can be created to notify people about the issue.

USE [DBADiagnostics]
GO

IF NOT EXISTS(SELECT * FROM sys.schemas WHERE name = 'Monitor')
    EXEC('CREATE SCHEMA [Monitor] AUTHORIZATION [dbo]')
GO

IF OBJECT_ID('Monitor.FileGrowth') IS NULL
BEGIN
    CREATE TABLE [Monitor].[FileGrowth]
        (
        DatabaseName sysname NOT NULL,
        DatabaseFileName nvarchar(260) NOT NULL,
        FileSizeMB decimal(18,3) NOT NULL
        CONSTRAINT PK_MonitorFileGrowth PRIMARY KEY(DatabaseName, DatabaseFileName)
        )
END

IF OBJECT_ID('Alert.FileGrowth') IS NOT NULL
    DROP PROCEDURE [Alert].[FileGrowth]
GO

/*================================================================================
Procedure:    [Alert].[FileGrowth] 
Author:        Jason Strate
Date:        2007-11-14

Synopsis:
    Procedures monitors the size of each file for all user databases and tempdb.  In
    the event of file growrh for any of the monitored databases, a error is raised
    that can be captured through either tools monitoring SQL Servers log files or 
    through SQL Agent Alerts.
         

================================================================================
Revision History:
Date:        By            Description
----------------------------------------------------------------------------------
================================================================================*/
CREATE PROCEDURE [Alert].[FileGrowth] 

As

SET NOCOUNT ON

-- Validate that necessary error message exists
IF NOT EXISTS (SELECT * FROM master..sysmessages WHERE error = 70000)
    EXEC master..sp_addmessage  @msgnum = 70000, @severity = 12, @with_log = 'true', 
        @msgtext = 'File growth has occured in the database %s on the file %s.  The size has increased from %d to %d.  If the file growth was unplanned, please review for unexpected issues.', @replace = 'REPLACE'

-- Insert files for each database that are not currently being watched
INSERT INTO [Monitor].[FileGrowth] (DatabaseName, DatabaseFileName, FileSizeMB)
SELECT d.name, mf.name, CAST(mf.size as float)*8/1024
FROM sys.databases d
    INNER JOIN sys.master_files mf ON d.database_id = mf.database_id
    LEFT OUTER JOIN Monitor.FileGrowth fg ON d.name = fg.DatabaseName AND mf.name = fg.DatabaseFileName
WHERE d.state_desc = 'ONLINE'
AND source_database_id IS NULL
AND d.name NOT IN ('model', 'master', 'msdb')
AND fg.FileSizeMB IS NULL

-- Delete databases that no longer exist
DELETE FROM Monitor.FileGrowth 
FROM Monitor.FileGrowth fg 
    LEFT OUTER JOIN sys.databases d ON fg.DatabaseName = d.name
WHERE d.name IS NULL

DECLARE @FileGrowthDelta table
    (
    DatabaseName sysname
    ,DatabaseFileName nvarchar(260) 
    ,OldFileSizeMB decimal(18,3)
    ,NewFileSizeMB decimal(18,3)
    )

-- Update files that changed size and output delta rows
UPDATE fg
SET FileSizeMB = CAST(CAST(mf.size as float)*8/1024 as decimal(18,3))
OUTPUT INSERTED.DatabaseName, INSERTED.DatabaseFileName, INSERTED.FileSizeMB, DELETED.FileSizeMB
INTO @FileGrowthDelta
FROM Monitor.FileGrowth fg 
    INNER JOIN sys.databases d ON d.name = fg.DatabaseName 
    INNER JOIN sys.master_files mf ON d.database_id = mf.database_id AND mf.name = fg.DatabaseFileName
WHERE d.state_desc = 'ONLINE'
AND source_database_id IS NULL
AND d.name NOT IN ('model', 'master', 'msdb')
AND FileSizeMB <> CAST(CAST(mf.size as float)*8/1024 as decimal(18,3))

--Declare variables section for triggering error event
DECLARE @DatabaseName sysname
    ,@DatabaseFileName nvarchar(260) 
    ,@OldFileSizeMB int
    ,@NewFileSizeMB int

--For each database name in sysdatabases
DECLARE ALTER_FILE_GROWTH_CURSOR CURSOR LOCAL FAST_FORWARD FOR 
    SELECT DatabaseName 
        ,DatabaseFileName 
        ,OldFileSizeMB 
        ,NewFileSizeMB
    FROM @FileGrowthDelta

OPEN ALTER_FILE_GROWTH_CURSOR
FETCH NEXT FROM ALTER_FILE_GROWTH_CURSOR INTO @DatabaseName, @DatabaseFileName, @OldFileSizeMB, @NewFileSizeMB

WHILE @@FETCH_STATUS = 0
BEGIN
    RAISERROR(70000, 0, 1, @DatabaseName, @DatabaseFileName, @OldFileSizeMB, @NewFileSizeMB) WITH LOG
    FETCH NEXT FROM ALTER_FILE_GROWTH_CURSOR INTO @DatabaseName, @DatabaseFileName, @OldFileSizeMB, @NewFileSizeMB    
END

CLOSE ALTER_FILE_GROWTH_CURSOR
DEALLOCATE ALTER_FILE_GROWTH_CURSOR
GO

EXEC [Alert].[FileGrowth]

In the next post, I’ll outline the process that I use to check to see if there are any files that may need be running out of available space.

July Presentations

Tuesday, June 30th, 2009

I know of three presentations coming up in the next month that I thought I’d share with everyone, if only because I’m involved with all three.

The first is Andy Leonard’s presentation on Incremental Loads, Change Data Capture, and SSIS 2008 in scenic Columbus, OH. The event takes place at 6:30PM on July 9th at Battelle for Kids. Lest you think Andy will be in Columbus, he will be presenting via LiveMeeting. This means that you can attend via LiveMeeting!

Presentation the second is Aaron Alton’s presentation on Simple Data Change Tracking in SQL Server. This is a virtual presentation that is happening on July 15th at 2PM Eastern.

Finally, on July 23rd I will be presenting at the Central Ohio .NET Developers Group. The event starts at 6:00 PM and it located at Microsoft building on Polaris Parkway in Columbus, Ohio. My presentation is titled From Tables to Objects: Making Your Database Work With You – it’s all about using database features to enhance the quality of your ORM. There is no LiveMeeting available for this, but I will either be recording via Camtasia or else will record it after the fact.

Google Reader Tutorial Video

Tuesday, June 30th, 2009

Google Reader is an RSS reader tool that helps you stay in touch with more web sites in less time. It consolidates new articles from sites all over the web in one single, easy-to-use interface, and helps you share your favorite articles with your friends. Brent Ozar explains Google Reader this four minute video tutorial.

Get the Flash Player to see the wordTube Media Player.

In the videos, Brent talks about these sites:

  • Google Reader – the online RSS reader tool.
  • Brent’s Shared Google Reader items – if you want to get notified of his favorite SQL Server blog posts.
  • AideRSS PostRank – the best/great/good filtering plugin for Reader that helps you catch up with the most popular blog entries quickly.
  • URLFan – find out when other people mention your blog or link to it.
  • Search.Twitter.com – find out when anyone mentions your name or your site’s name on Twitter.

Subscribing or Downloading the Podcast

If you have an MP3 player or a portable video player and you want to download our videos automatically, you can subscribe to our podcast feeds here:

You can also download this video to watch it later:

Letter to the Editors & Contributors for June 30th

Tuesday, June 30th, 2009

This week, we have some developments that make me proud to wear the big hat around here.

Comments On Your Stories Belong On Your Site

Brett Epps blogged about the latest round of improvements to SSP.  Most of ‘em focus on making the site load faster, but my favorite one has nothing to do with performance.

As of today, syndicated blog posts don’t have a separate comments area on SQLServerPedia.  If visitors want to comment about your blog entry, they’ll click on a link to go to your own web site.  This isn’t just a switch you can throw in WordPress, either – we couldn’t have done it without Brett’s PHP/MySQL work under the hood.

This keeps all the discussion about a post in a single place, which makes life easier for bloggers.  Readers can interact better with each other regardless of where they found your blog initially.  Plus, if like me, you’ve implemented a Subscribe to Comments plugin for your blog, then readers will get notified whenever anyone else posts a comment too.  Since all comments will be happening in one place, it makes readers more likely to return as new comments are posted in one place.

This also perks up the activity on your blog.  As a blogger, I know it can be frustrating to put a lot of work into an entry and then not get any comments.  If comments ended up on SQLServerPedia, that’s cool, but it’s not as cool as having them end up on your own site.  Then as readers stumble across your site, it’ll look more active.

I don’t want to syndicate your site’s comments over to SQLServerPedia because I want you, the blogger, to have incentives you can use to bring traffic to your own blog.  To make it easier, use FeedBurner FeedFlare to add comment links to the bottom of your posts, because FeedBurner can automatically update the number of comments.  I talk about using FeedBurner FeedFlare in my Tips for Syndicated Bloggers.

Solving Plagiarism Problems

A web site popped up on the radar recently for attempting to plagiarize just about everyone in the database community.  They used RSS feeds to suck the content out of blogs and show it on their own site without any attribution whatsoever.  Several of us bloggers contacted the site and demanded that our content be taken down, and Quest’s legal department sent cease & desist letters to the site.  Eventually, the site’s hosting company intervened after we began sending DMCA violation notices, and the site’s down for the count right now.

For more in-depth coverage of the story, check out my posts on How to Take Action When Your Site is Plagiarized and the followup More Thoughts on Blog Plagiarism.

Is Your Biography Up to Date?

No, we’re not worried about writing your obituary, even though the #celebpocalypse keeps claiming more victims.  Instead, we’re kicking around the idea of including your bio from the SQLServerPedia Editors and Contributors page at the bottom of every blog entry you write.  If there’s anything you’d like to update, take a moment to go edit that page.  Try to resist the temptation to edit the bios of others.

squeaky wheel… yada, yada

Monday, June 29th, 2009

Well, I guess if you complain about something long and hard enough, things happen.  Sometimes, things happen that have nothing to do with your complaining, but the coincidental correlation is so strong that your mind draws associations for it (which sometimes leads to fodder for blog posts).

BTW, sorry for not posting much lately; my personal life has gotten rather complicated lately, so I’ve been avoiding the computer after hours.

Anyway, I’ve been struggling for some time now with Visual Studio for Team Systems: Database Edition, particularly in a scrum environment, and it just seems that I’m not making much headway with it.  It’s just a foreign model of usage, and I’m still struggling with the tool. It’s kind of like learning to drive a car after driving a golf cart; the basic principles are the same, but there’s a heckuva lot more options.

Anyway, Shawn Wildermuth to my rescue; his company, Agilitrain, is hosting a series of workshops on Agile Database Techniques using Visual Studio 2008 Database Edition; I told my boss about it, and even though training money is pretty tight right now, I got approval to go within minutes (that says a lot about my boss, or it may say something about his opinion of me and the need for training).  Anyway, here’s the course description:

Agile Database Techniques

This course provides students with the knowledge and skills to properly manage the SQL Server database development lifecycle in an agile environment. The student will learn how to manage changes to the database structure, ensuring quality through T-SQL unit testing, and how to automate the building and deploying of SQL Server databases. Intended for developers and administrators already comfortable with the concepts of designing, programming, testing, and deploying SQL Server databases, this course focuses on using Visual Studio Team System 2008 Database Edition to maximize productivity while minimizing mistakes.

Price: $1995.00 (USD)
Pricing may be different in locations outside the United States
Language(s): English
Length: 3 Days

Pre-requisites:

Before attending this course, the student should have experience developing and maintaining SQL Server 2000, 2005, or 2008 databases. It would also be beneficial if the student has worked in a team-based software development project and is familiar with their organization’s Software Development Life Cycle.

I’ve also been notified that Nob Hill Software has released their latest version of Randolph (an automatic versioning software for databases) with full support for TFS.  Unfortunately, I haven’t had a chance to install and test it yet (I’ve actually been working on an Analysis Services project; looks like I’m becoming a BI guy), but once I do, I’ll post a review here and at the AtlantaMDF Review site.

Undelete for SQL Server tables?

Monday, June 29th, 2009
I had a good friend recently ask me if there was any way to undelete a table that had accidentally been dropped from a database.

My answer was basically, no there isn't.

Or to be more specific, the answer to that question is equivalent to the answer to this one: "How prepared is the db or the system for disaster recovery?"

In theory there are plenty of options, but they all hinge on how prepared the system was for an event in advance: This could cover a large number of areas including backups, snapshots, mirrors and replicated dbs. Not to mention hardware redundancy solutions that may have been put in place.

Silly question?
But it's not a silly question. Not at all. There are a number of Undelete programs available for file systems. They all operate under the disclaimer count yourself lucky to get any data back.

So the natural question is: Is there anything fundamentally blocking someone from trying to write an undelete program for tables? With the obvious disclaimers that any data you might get back is dirty as a pickup.

I decided to find out. I created a table with a number of rows and deleted it. The pages of data that used to contain the table are still there, but it's marked as unused and available. That's promising, the (dirty) data is still there, it just doesn't have any structure. Then I looked at the IAM (index allocation map) page. As far as I know, this is the page the db looks at to find out the most fundamental information about the structure and location of an index. This particular page gets zeroed out.

So yes, as I expected, with SQL Server, there is something fundamentally blocking a user or program from performing undelete on a table.

Best Practices
So looks like best practices prevail. Keep data backed up if you need it. Even if it's a quick and dirty database. A quick and dirty backup takes 5 extra clicks than saving query text to file with "Save As...".

The Best Thing I learned at PASS Summit

Monday, June 29th, 2009

Wow, what a great question!

The Best Thing I Learned at PASS Summit

I vividly remember my first Summit experience. I was sitting with a couple of hundred other people in a session. The speaker was an established industry expert and author. I had a couple of his books on my shelf at home and I was eager to hear him in person. He was doing a great job when someone asked a question. It was obvious he didn’t know the answer – that’s okay no one knows everything. He handled it well. Just then another SQL legend in the audience stood up and gave the answer. As that person finished, a Product Manager for SQL Server stood up and provided even more insight for the future direction of SQL Server.

I remember sliding down in my seat thinking “There’s no way I’ll ever speak at a conference like this. So, I’ve got to find another way to volunteer for PASS.”

I did. I became a chapter manager, then ran for and was elected to the Board of Directors, and eventually became the Executive Vice President of Finance for PASS. During my 9 years of volunteerism (6 years on the Board), PASS afforded me many opportunities to do things that I would not have had a chance to do otherwise as a self employed database consultant, far too many to list! And for that, I’m very thankful.

So what did I learn?  Some call it the Law of the Harvest. The more you put in, the more you tend to get back. The more you give, the more you receive.

And oddly enough since that first experience, I’ve given more than 50 sessions at conferences throughout North America and Europe.

PASS Board of Directors at the PASS European Conference 2005 in Munich

It’s Your Turn!

Got a PASS Summit story? I’d love to hear it. And so could PASS. Jot it down in a blog or email and you would win a trip to this year’s Summit in Seattle or some other prizes. For more details visit the contest site.

Cheers!

Joe

Data Mining Basics with Tom LaRock

Monday, June 29th, 2009

We’ve talked about data mining the StackOverflow database dump before, but today we’re going to start from the very beginning.  In this half-hour video, Tom LaRock and Brent Ozar talk about what data mining is, and show some examples using the StackOverflow database.

Get the Flash Player to see the wordTube Media Player.

For more about importing the StackOverflow database export and working with it, check out our articles on data mining the StackOverflow database dump.

Subscribing or Downloading the Podcast

If you have an MP3 player or a portable video player and you want to download our videos automatically, you can subscribe to our podcast feeds here:

You can also download this video to watch it later:

Speaking Engagements

Monday, June 29th, 2009

It’s going to be a busy 6 months in terms of conferences and speaking (well, busy for me, I’m just getting used to the whole speaker thing)

TechEd Africa

TechEd Africa is running from the 2nd to the 5th of August in Durban. I’m presenting two sessions there, one on query hints and plan guides and one on evaluating your indexing strategies. I’ll also very likely be helping out in the community lounge and the Ask the Experts area.

PASS Community Summit

I’m presenting two sessions at the PASS Community Summit this year. A spotlight session on titled ‘Lies, damned lies and Statistics’ and a general session titled ‘Insight into Indexes’

The spotlight session will be covering column statistics, why SQL creates them and how, when they’re updated, the importance of accurate statistics, some of the problems that can result when they’re not accurate and some maintenance strategies.

The general session will be looking at what SQL can tell you about indexes, how they’re been used, what they’re been used for and what indexes SQL thinks it wants, and how reliable all that information is.

SQL Usergroup

I’ll be presenting at the October meeting of the SQL Server usergroup and will be doing a final dry-run of one of the presentations that I’ll be giving at PASS. I haven’t decided which one yet.