Posted by: sqlswimmer | March 22, 2017

SQL Saturday Richmond (#610)

On Saturday, March 18, 2017, I spoke at my very first SQL Saturday.  I have been an attendee, involved in organizing and volunteered at many over the years, but this was the very first time I was a speaker.  My session, What is Power BI?

I have presented this session twice before, once to my local user group and once at the Triad Developers Conference, so I was fairly comfortable with my content.  Richmond had 245 attendees registered with 5 different session tracks.  I had about 25 people in my session.  Of those 25, I only saw one nod off, but it was the first session of the day (8:30am), so I’m going to chalk that one up to not enough caffeine prior to the session.  There were some great questions and I had several people approach after the session with more detailed questions and to tell me how much they enjoyed my session.  Some were so excited they would be able to take action when they got back to work on Monday based on my session.  It really doesn’t get any better than that.

With my session in the rear view, I was excited to attend other sessions.  I was able to make it into two other sessions, both of which were fabulous.  The organizing team for SQL Saturday Richmond did a fabulous job.  The event appeared to run like clockwork, I think maybe they’ve done this before Winking smile

One thing I really liked about this event is that they did not have a typical speaker dinner and gift, they did a speaker event.  It was at G-Force Karts which was so exciting for me because I’d never driven a go kart before.  I’ve always fancied myself a race car driver in another life (much to Martin’s dismay) so this was my opportunity to see if it was true.  All I can say is, “Yes, it’s true.”  I had so much fun.  I wish more SQL Saturday organizers would consider doing something like this.  A nice dinner is always appreciated and a gift is a thoughtful gesture, but the memories I made with my #SQLFamily at G-Force Karts are something I will NEVER forget.

I just want to thank the organizers of SQL Saturday Richmond, all the volunteers, sponsors and spouses who made this event happen.  It was truly amazing and something I will remember my entire life.  Well done.

SQLSatRichmondGoKart

Photo courtesy of Doug Purnell (Blog |Twitter)

Advertisements
Posted by: sqlswimmer | March 16, 2017

Check Those Settings

Recently, I was tasked with “enhancing” a third party application.  This third party application (TPA) outputs a bunch of files to a file share in a way that makes sense to the application, but makes no sense to a human. 

The Task

Make a copy of these files in a new location that makes sense to humans.

The Rules of Engagement

  • Do not modify any of the existing files or file structures created by the TPA.
  • Do not modify any of the TPA database objects.
  • Do not add any objects to the TPA database.

First thing that popped into my head was, “I can do that in PowerShell in less than 5 minutes.”  Kind of like the old game show Name That Tune, my confidence level was high.  Little did I know what was in store for me.  Less than 5 minutes turned into more than 5 hours.

In order to make the files and file structure make sense to a human, I had to use a stored procedure from the TPA to decode some bits.  Easy enough in PowerShell, just use my favorite command-let from dbatools.io, invoke-sqlcmd2.  But wait, one minor detail, I am not allowed to install any snap-ins or any other tools on the server where this would run.  In fact, I can’t even use the latest version of PowerShell, I am stuck with using PowerShell 2.0 <sigh>.

After I dusted off my PowerShell 2.0 documentation, I got my script written and started testing.  I processed several folders and their files before I received the following error while running my PowerShell script:

Invoke-Sqlcmd : String or binary data would be truncated.
The statement has been terminated.
At line:127 char:36
+ … MyResults = Invoke-Sqlcmd -ServerInstance $ServerName `
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

Interesting.  I added some Write-Host statements for troubleshooting and found the offending entry.  Like any good programmer, I tested my stored procedure call in SQL Server Management Studio (SSMS) to make sure it really was a SQL Server error and guess what?  It worked just fine!  No errors what so ever.  WTH?!  This is where my tunnel vision sets in.  If it works in SSMS but not in PowerShell, then PowerShell must be the problem, right?  Well, sort of.

After repeatedly running the same piece of code and expecting different results (yes, like I said, tunnel vision), I threw my hands up and quit for the day.  I had restless dreams that night.  I was being chased by a giant SQLString Truncator (a very rare dinosaur from esoteric era).  I woke with a start at 4:30am, I had to be missing something.  All I can say is, thank goodness for Twitter and #SQLHelp.  I tweeted my issue and got immediate responses from some very smart folks, but nothing that resolved my issue, until I read between the lines of a tweet from Robert Davis:

SQLSolderTweet

That’s when the light bulb went on and Robert sent his follow up tweet:

SQLSolderTweet2

I copied all my settings from SSMS and added them to my PowerShell script.  One by one I commented them out until I was left with just one.  Low and behold that SQLString Truncator was really one of those pesky ARITHABORT Biters.

Lessons Learned

  • As soon as that tunnel vision kicks in, you need to stop what you are doing and take a break.
  • Ask for help, don’t keep beating your head against the wall.
  • Most importantly, don’t forget about your settings.  They can make all the difference in the world.
  • ARITHABORT Biters are much harder to catch in the wild than SQLString Truncators.
Posted by: sqlswimmer | March 11, 2017

Next Stop, SQL Saturday Richmond

I can’t believe it’s almost time for SQL Saturday #610.  I’ll be there presenting What is Power BI?  If you are in the Richmond area Saturday, March 18, 2017, please stop by and say “Hello”, I’d love to see you.  There are tons of other sessions as well, so sit a spell and get your SQL Learnin’ On!

Posted by: sqlswimmer | March 10, 2017

Triad Developers Conference – My Debut

I did it!  I did my first “real world” presentation this morning at the Triad Developers Conference in Winston-Salem.  What I mean by “real world” is not a PASS audience.  These were total strangers off the street that I didn’t know, well, there were some familiar faces and even a friend or two, but for the most part total and complete strangers that had varying backgrounds, not all technical in nature.

The feedback I received was very positive and even helpful, so I can make this presentation even better when I present it in Richmond, VA next weekend at SQL Saturday #610.

Huge thank you goes out to the organizers, volunteers and sponsors who made this event happen.  And a special thank you goes out to Doug Purnell (Blog | Twitter) for recommending me in the first place.

Posted by: sqlswimmer | February 9, 2017

Speaking at Triad Developer’s Conference

I am honored to have been recommended by a colleague and selected as a speaker for the Triad Developer’s Conference in Winston-Salem on March 10, 2017.

The Triad Developer’s Conference is a low cost one day learning event put on by a myriad of local user groups in the Piedmont Triad area of North Carolina.  I attended the inaugural conference two years ago and it was fantastic.  So excited to be a speaker this year.

If you are in the Winston-Salem area on Friday, March 10, 2017, stop and say “Hi”.  I’d love to see you.

Posted by: sqlswimmer | January 31, 2017

Pesky Percent File Growth

As DBAs we all know setting your file growth to grow by percent is not optimal.  It can cause all kinds of issues, which rear their ugly heads as performance problems (see these articles by Brent Ozar, & Tim Ford).  So, when I have to support a third party application that automatically adds data files using percent instead of fixed size, it really irritates me.  I got tired of seeing these new files show up on my daily exceptions report, so I decided to do something about it.  This post explains what I did.

I have a home grown process that goes out and collects all kinds of information about my servers on a daily basis.  Once that process is complete it sends reports, via email, so I can get a quick look at things when I first arrive at work in the morning.  One of those reports is my file exception report.  It reports things like excessive data/log file growth, data/log files that are almost full, data/log files that use the percent file growth, etc.  The first time I had a file show up on my exceptions report with a percent file growth, I decided I needed to be notified before the report landed in my inbox, so I created a server level trigger that is triggered by the ALTER DATABASE command.  This trigger captures all the relevant information and sends me an email.  Here’s the code I used for my trigger:

CREATE TRIGGER [ddl_trig_alterdatabase]
ON ALL SERVER
FOR ALTER_DATABASE
AS
   DECLARE @Subject nvarchar(255)
      , @Body nvarchar(MAX)
   SELECT @Subject = N’A database was altered on ‘ + @@Servername
      , @Body = EVENTDATA().value(‘(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]’,’nvarchar(max)’)
   exec msdb.dbo.sp_send_dbmail
      @recipients = ‘myemail@emaildomain.com’, — varchar(max)
      @subject = @Subject, — nvarchar(255)
      @body = @Body
GO
ENABLE TRIGGER [ddl_trig_alterdatabase] ON ALL SERVER
GO

 

This worked great, I found out before my report showed up and I could address the issue when it happened.  Unfortunately I discovered that one of the applications was making this change in the middle of the night.  I certainly didn’t want to have to wake up in the middle of the night to address this issue, since it really isn’t a “production down” type of problem (and let’s face it, no DBA wants to be woken in the middle of the night for anything, let alone something that is not production down).

I decided I needed to do something else other than just send an email notification, I needed to take corrective action when it occurred.  So I wrote a little stored procedure that will take the ALTER DATABASE statement as a parameter, parse it and take the appropriate corrective action. 

Simple enough, right?  Now I just need to add the call to my newly created stored procedure in my server level trigger and we are good to go.  But wait, you can’t ALTER a database within an ALTER DATABASE statement (don’t believe me? Use this as a learning exercise to see what happens when you try).  So what could I do?  There are several things you could do, but I chose to create a table that could hold this newly created ALTER DATABASE statement and insert the record there.  Then I created a SQL Agent job that runs once every hour and reads that table and executes any entries it finds, then deletes them after successfully executing.

Here’s the code for my stored procedure:

CREATE PROCEDURE [dbo].[ChangePercentGrowthMaxSizeUnlimited]
@SQLText nvarchar(max)
AS
SET NOCOUNT ON

/* We start with something like this
ALTER DATABASE [DatabaseName]
ADD FILE (NAME = N’DataLogFileName’
         ,FILENAME = N’X:\DataLogFileName.ndf’
         , SIZE = 20
         , FILEGROWTH = 5%
         , MAXSIZE = UNLIMITED)
*/

/*  We want to produce something like this
ALTER DATABASE [DatabaseName]
MODIFY FILE ( NAME = N’DataLogFileName’
            , MAXSIZE = 102400KB
            , FILEGROWTH = 10240KB )
*/

— Local Vars
DECLARE @AddFileText VARCHAR(8) = ‘ADD FILE’
   , @ContainsAddFileText BIT = 0
   , @AddFileStartPosition BIGINT
   , @FileGrowthPercentText VARCHAR(13) = ‘FILEGROWTH = %!%’
   , @ContainsFileGrowthPercent BIT = 0
   , @ContainsMaxSizeUnlmitedText BIT = 0
   , @StartPosition INT
   , @EndPosition INT
   , @Length INT
   , @DatabaseName VARCHAR(128)
   , @FileName VARCHAR(128)
   , @AlterDatabaseLength INT = LEN(‘ALTER DATABASE ‘)
   , @AlterDatabaseSQL NVARCHAR(MAX)

   — Is it an ADD File operation?
   SELECT @AddFileStartPosition = PATINDEX(‘%’ + @AddFileText + ‘%’, @SQLText)
  
   IF @AddFileStartPosition > 0
   BEGIN
      –It’s an ADD File operation
      SET @ContainsAddFileText = 1

      IF @SQLText LIKE ‘%’ + @FileGrowthPercentText + ‘%’ ESCAPE ‘!’
      BEGIN
         — it’s adding a file using percent file growth
         SET @ContainsFileGrowthPercent = 1
     
         — Is it setting MAXSIZE to UNLIMITED?
         IF PATINDEX(‘%’ + @MaxSizeUnlimitedText + ‘%’, @SQLText) > 0
         BEGIN
            SET @ContainsMaxSizeUnlmitedText = 1
         END
        
         — Now we need to parse the ADD FILE expression and build a MODIFY FILE operation from the parts
         — Get database name
         SELECT @StartPosition = @AlterDatabaseLength + 1
         SELECT @Length = CHARINDEX(‘ADD FILE’, @SQLText, @AlterDatabaseLength) – @AlterDatabaseLength
         SELECT @DatabaseName = LTRIM(RTRIM(SUBSTRING(@SQLText, @AlterDatabaseLength + 2, @AddFileStartPosition – 1 – @AlterDatabaseLength – 2)))

         — Get filename
         — Start by finding the start of the logical file name
         SELECT @StartPosition = CHARINDEX(””, @SQLText, PATINDEX(‘%’ + ‘[^FILE]NAME %”’ + ‘%’, @SQLText)) + 1
         SELECT @Length = CHARINDEX(””, @SQLText, @StartPosition) – @StartPosition
         SELECT @FileName = SUBSTRING(@SQLText, @StartPosition, @Length)

         — Now Create the alter database operation
         SELECT @AlterDatabaseSQL = N’ALTER DATABASE ‘ + @DatabaseName + N’ MODIFY FILE ( NAME = N”’ + @FileName + N”’, ‘
         IF @ContainsFileGrowthPercent = 1
            SELECT @AlterDatabaseSQL = @AlterDatabaseSQL + N’FILEGROWTH = 10240KB’

         IF @ContainsMaxSizeUnlmitedText = 1 AND @ContainsFileGrowthPercent = 1
            SELECT @AlterDatabaseSQL = @AlterDatabaseSQL  + ‘, MAXSIZE = 102400KB’
         ELSE
            IF @ContainsMaxSizeUnlmitedText = 1 AND @ContainsFileGrowthPercent = 0
               SELECT @AlterDatabaseSQL = @AlterDatabaseSQL + N’MAXSIZE = 102400KB’
           
         SELECT @AlterDatabaseSQL = @AlterDatabaseSQL + N’ )’

         INSERT dbo.DBAAlterDatabase
         (SQLText)
         VALUES
         (@AlterDatabaseSQL)
      END
   END
   ELSE
   BEGIN
      — It’s not an ADD FILE operation
      PRINT ‘It”s not an ADD FILE operation, no work to do.’
   END

RETURN 0

 

Here’s the code for my modified server level trigger:

CREATE TRIGGER [ddl_trig_alterdatabase]
ON ALL SERVER
FOR ALTER_DATABASE
AS
   DECLARE @Subject nvarchar(255)
      , @Body nvarchar(MAX)
   SELECT @Subject = N’A database was altered on ‘ + @@Servername
      , @Body = EVENTDATA().value(‘(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]’,’nvarchar(max)’)
   exec msdb.dbo.sp_send_dbmail
      @recipients = ‘myemail@emaildomain.com’, — varchar(max)
      @subject = @Subject, — nvarchar(255)
      @body = @Body 
   EXEC dbo.ChangePercentGrowthMaxSizeUnlimited @SQLText = @Body
GO
ENABLE TRIGGER [ddl_trig_alterdatabase] ON ALL SERVER
GO

Works like a charm!  But wait, you might notice that I’m making more than a few assumptions in my stored procedure, and you would be correct.  I feel like I need to add a disclaimer to this post, the same way they add disclaimers to pharmaceutical commercials.

Here are my assumptions: 

  1. Only one file is being created at a time. 
  2. We always want to change our file growth to 10MB. 
  3. We don’t want a max file size of unlimited and we always want to set our max file size to 100MB. 
  4. There will be no errors.
  5. I am in NO way responsible if this code breaks something on your server.

Explanation of assumptions:

  1. What fun would it be if I did all the hard work for you?  This can easily be adapted to work with multiple files being created at the same time.  You can do it, I have faith in you.
  2. For my particular instance, I know the model database settings for this server and I hard coded them, because that’s what I wanted.  You could easily adapt the code to use your model database settings or any other value for that matter (HINT: think sys.sysfiles).
  3. See explanation of assumption 2 above.
  4. I removed all my standard stored procedure framework code (which includes error checking) for brevity.  You should ALWAYS have error checking in your stored procedures!
  5. You should NEVER assume code is not malicious in nature and add it to production without a thorough understanding of what it’s doing.  Shame on you if you did.

 

Posted by: sqlswimmer | January 31, 2017

My First SQL Saturday – As A Speaker!

I am so honored and excited to have been selected to speak at SQL Saturday Richmond on March 18, 2017.

I will be presenting my What is Power BI? session.  There have been a ton of changes to Power BI since I last presented this, so I’m off to start updating my slide deck. 

If you are close to Richmond, VA on March 18, 2017 and want to learn more about what Power BI is, please register for this event and stop by and see me.  I’d love to have you as would the SQL Saturday Richmond team.

Posted by: sqlswimmer | January 23, 2017

How to Exclude Entire Sections in VS Schema Compare

Right now I am so excited, and a little embarrassed.  After using Visual Studio (VS) for database projects for the last 7 years, I am just now finding out about this feature?!  What feature is that, you ask?  Let me tell you…

When using VS for database projects I typically use my environment specific Publish xml file to deploy changes to my local database when experimenting with code changes.  However, every once in a while I will have to use the New Schema Compare tool from the Tools | SQL Server menu when I have a “one-off” database that I need to synch to my database project.

Quite frequently because these are one-off databases there will be a ton of junk items that are in the database, but not in my database project or lots of objects in my project that aren’t in my database.  Either way I want to ignore those changes.  In the past, I’ve always manually unchecked each item, tedious when you have more than two items to uncheck.

SQLSchemaCompare2

I always thought there should be a better way than having to manually uncheck each object, but never thought more about or how to do it.

Well, today I figured it out.  All you have to do is right click on the section and ta-da, you can Include or Exclude all objects depending on the existing state of the objects.  So in the image below, I right click on Delete, select Exclude

SQLSchemaCompare3

and I’m left with the following:

SQLSchemaCompare4

In just one click instead of 14 (if I wanted to exclude all the table drops).

Like I said, I have no idea why it took me so long to figure this out, but surely I can’t be the only person who didn’t know this, so now you do too.

Posted by: sqlswimmer | October 12, 2016

So Long, Farewell, I Hate to Say Goodbye

The Sound of Music is one of my all time favorite movies.  It reminds me of some great times with family during the holidays when I was a kid.  The adults would all be in the living room talking about “boring adult stuff” and all us kids would be piled in the family room on the floor with the fire place roaring and singing along at the top of our lungs to every single song. 

Those are some great memories and those memories are what make us “us” and that is why this post if very hard for me to write.  My mother has been officially diagnosed with Alzheimer’s and it has been devastating for my family.  

Most of you know I do lots of volunteer work for PASS.  It’s one of my PASSions, I love working with folks in our #SQLFamily and the folks at PASS HQ.  However, with my mother’s diagnosis, something has to give and unfortunately my volunteer work with PASS is one of the things that will be affected.  I will be scaling back my volunteer efforts with PASS. 

Currently I serve on the Program Committee as a Program Manager and am a PASS chapter leader, among other various volunteer things throughout the year.  I have been on the Program Committee for the last seven years, spending the last two as a Program Manager.  We have come a long way from where we were when I started seven years ago, but there’s always room for improvement.  I really wish I could be part of the team that keeps making it better, but my family needs my attention now, so I will be stepping down from the Program Committee.  I’ve made some good friends along the way and am thankful for the opportunity I have had to give back to the community by being part of this great team.  I will continue to serve as the Chapter Leader in Greensboro and various other opportunities when they come up, however those opportunities will be in a limited capacity now.

I have some great memories of my PASS family which is why I really hate to say goodbye, so I’m going to think about raindrops on roses and whiskers on kittens, bright copper kettles and warm woolen mittens..then maybe I won’t feel so bad.

Posted by: sqlswimmer | September 1, 2016

SQL Saturday Charlotte is Coming!

That’s right, SQL Saturday Charlotte is coming September 17, 2016.  Next to the annual PASS Summit, this is my favorite SQL Event!  This is the fifth year that the Charlotte BI Group (CBIG) has put on this great event.  They’ve expanded the event this year to include two Pre-Cons on Friday, September 16, 2016, one from BI and data analytics queen Jen Underwood and the other from performance guru Adam Machanic.  While the Pre-Cons aren’t free like the event on Saturday, they certainly are a bargain for an entire day of training with well known experts in their respective fields.

If you haven’t already registered, I’d suggest you do it now before they go to a wait list.  This event is always jam packed full of great sessions from great speakers, both local and national.

Oh yeah, I will be there.  I’m helping out again this year with registration, so be sure to say hello if you see me.  I am always happy to see my #SQLFamily.

« Newer Posts - Older Posts »

Categories