Taxes have been filed

I’m one of those people who likes to address tax season as soon as I have all my ducks in a row. I don’t like to let it linger to far into February if I can help it but by the end of January is my primary target. Thank goodness that a lot of companies have switched to paperless delivery. This makes being organized that much better. Being on a first name basis with your tax accountant doesn’t hurt either. As soon as I had all the documents, receipts, spreadsheets, etc… I merely emailed them to Rita and by the end of the day on Friday all I needed to do was to show up and sign.

I switched up the way I deal with our withholding. Before, like many others, I would contribute the max which would later yield a sizable tax return without bearing interest. Being a bit more smart about money in general and knowing the power of compound interest I aim to break even at the end of the year. A small return is better than having to pay so I work with my account to make sure my tax goals are aligned before the end of the year. This makes more money available on a monthly basis which allows me to take advantage of interest benefits from other investments. I see it as some interest made is better than none at all.

Back with a new agenda

It has been a while. Many things have changed. Priorities, interest, life, focus. I was so caught up in the technical field that I let a lot of other interesting and rather important topics slide by. I’ve since undergone a rebalance and decided to expand my horizon a bit more.

I’m still a SQL Server DBA but I’ve also been neck-deep in family, finances, aquaponics, aquaculture, micro-farming, health, fitness, food, real estate, ios development while trying to keep up with taxes and investments. It’s pretty exhausting at the end of the day but it feels more complete.

Since my last post I’ve moved on from food industry to the health industry and it’s been an interesting learning experience. Very high paced, privacy intensive but I’m doing more than typical database administration. I’ve done a bit more with SSIS development and it’s been a great experience that I wish to continue.

With the use of YNAB I’ve been hitting the budget, reviewing our spending and making life changes to align us with financial freedom. A lot of eye-opening discoveries once you have the big picture of where your money is going every month.

My personal studies on investing leads to late nights but gives me more of a better understanding when it comes time to rebalancing. Something I never did before. This gives me more control over what I am investing in. Got to build a better nest egg.

Thanks to BitFountain, GangplankHQ and the iOS community I’ve been learning iOS development and have a few projects underway to better me in my journey. Nothing fancy but realistic applications. Hoping to have something in the apple store soon.

I’ve kicked up my gardening skills a bit. Implemented a good size aquaponics system that will perpetually feed a family of four. With aquaponics I am able to raise fish (for consumption) that provide a fertilizer for my fruits and veggies. Many dishes have been served with organic goodies from the aquaponics system.

Our little micro-farm now has five free-range, egg laying hens that are a year old. We collect 4-5 eggs a day from these gals. Our seven pecan trees produce about 200 lbs every year and our orange tree is cranking. We will be converting our old stored into a 12’x22′ greenhouse. This will hold our new aquaponics system which will be twice as large which will incorporate a fish farming setup.

Lots of things happening so it’s never a dull moment.

Consolidate multiple records into a single row

I worked on a restore script that had to consume values from the network share and produce a restore statement. This database happened to be striped to eight files. I needed a way to construct the restore statement into a single row and I was able to achieve the desired results using the following syntax.

	,colors VARCHAR(30)

INSERT INTO @temp (colors)

VALUES ('Red')

SELECT colors

FROM @temp

SELECT 'Colors' = 'RESTORE ' + 
(SELECT colors + ';'

FROM @temp 

FOR XML PATH (''), type).value('.', 'varchar(max)')

Here’s the end results.


Associating SSRS Scriptions to SQL Jobs

I’ve dealt with a lot of scenarios where I needed to troubleshoot and test a reporting services subscription simply because it failed. Most of the issues stem from the owner permissions, typos when it comes to the email addresses or because the person left the company and didn’t remove their subscriptions. Knowing where to look and how to associate the rather ugly naming convention of the sql jobs in reference to the subscriptions can eat up some valuable time. So let me save you some time by sharing a script I have used on numerous occasions.

The script joins a few ReportServer specific tables to sys.objects. I cannot recall if I had tested this against SQL 2005 but I do know it works well with SQL 2008 and it would be safe to say that it works with 2008 R2 as well. I am pretty certain it should execute without a hiccup on a 2005 instance. I haven’t tested against 2012 as of yet, but when I do I’ll definitely update this post.

To bread and butter. Here’s the script. The following will return all SQL Jobs associated to SQL Server Reporting Services subscriptions.

	cmd='EXEC msdb.dbo.sp_start_job @job_name= ''' + sj.[name] + ''';'
FROM msdb.dbo.sysjobs sj
	JOIN [ReportServer].[dbo].[ReportSchedule] AS rs (NOLOCK) ON sj.[name] = CAST(rs.[ScheduleID] AS NVARCHAR(128))
	JOIN [ReportServer].[dbo].[Catalog] c (NOLOCK) ON rs.[ReportID] = c.[ItemID]
	JOIN [ReportServer].[dbo].[Users] u (NOLOCK) ON c.[CreatedByID] = u.[UserID]
ORDER BY sj.[date_created] DESC

I like to know what was created today so I run the following:

	cmd='EXEC msdb.dbo.sp_start_job @job_name= ''' + sj.[name] + ''';'
FROM msdb.dbo.sysjobs sj
	JOIN [ReportServer].[dbo].[ReportSchedule] AS rs (NOLOCK) ON sj.[name] = CAST(rs.[ScheduleID] AS NVARCHAR(128))
	JOIN [ReportServer].[dbo].[Catalog] c (NOLOCK) ON rs.[ReportID] = c.[ItemID]
	JOIN [ReportServer].[dbo].[Users] u (NOLOCK) ON c.[CreatedByID] = u.[UserID]
WHERE (CONVERT(VARCHAR(10),sj.[date_created],101) = CONVERT(VARCHAR(10),GETDATE(),101))
ORDER BY sj.[date_created] DESC

The cmd column provides me the command needed to kick off the subscription. Typically most of the subscriptions are email based so if it hits my inbox I know I am good!

Intermittent Lockups with SSMS 2012

First off this post should have been pressed long ago (5/31/2012), but for whatever reason I merely saved it to draft rather than publish it. So without further ado…

Not long ago I started to encounter very intermittent lockups with SSMS 2012. I searched the web hi and low to see if anyone else had encountered this very problem, but I ended up with nothing remotely close. So I decided to turn to twitter and posted my issue to the sql community using the #sqlhelp hashtag.

It wasn’t long until I received a tweet from J. Verheul (‏@DevJef | Blog)

And that’s how our glorious conversation began… Then @DevJef mentioned he reinstalled SP1 for VS2010 and that cleared up his problem.

So I followed suit and applied SP1 for VS2010 and I am happy to report that I am no longer experiencing random lockups with SSMS 2012! Thank you @DevJef and thank you sql community for always being there!

Please visit Jef’s post (#SQLHelp – SQL 2012 Management Studio Freezes). His insight helped me tremendously and save me a lot of frustration and headaches!

Exporting Photos With BCP

I won’t get into all of the “ins and outs” about BCP, but feel free to read up on the subject here: Import and Export Bulk Data by Using the bcp Utility (SQL Server)


I have images stored in a SQL Server database and I need to access them without writing, purchasing or using a custom app. What can I do?


Step 1: Identify the table in question

In this case we are working with the Images table inside of the [devdb] database

Step 2: Generate a BCP format file

Create the format file by executing the following statement in a command prompt window.

bcp devdb.dbo.Images format nul -c -fimages.fmt -T -S (local)\DEV12 -v

This will produce a file named images.fmt in the C:\Temp\ directory

Step 3: Modify the format file

Open the newly pressed images.fmt with notepad. We will (very carefully) edit the contents. So here’s a look at the original file. Note: I modified the following image to fit the width of this layout. Meaning I cropped some white-space. Also note I have SQL 2005, 2008 R2 and SQL 2012 installed on my box. The 10.0 on the first line is present because the BCP version is from SQL 2008. It would show 11.0 if I were using BCP for SQL 2012.

Here is what it needs to look like after we make some adjustments. We modified the file to single out the image column. Be sure to SAVE your changes, especially changing SQLCHAR to SQLBINARY; otherwise, empty files will be created with a zero file size. Notice the white-space in the following image. I really don’t know if it makes a difference but the slight modifications I do make always work.

Step 4: Enable xp_cmdshell

If you don’t have xp_cmdshell enabled here’s the means to do so. Keep in mind if you don’t use xp_cmdshell often then be sure to disable it when you’re finished. You can search the subject in respects to security and xp_cmdshell.

EXEC sp_configure 'Show Advanced Options',1;
EXEC sp_configure 'xp_cmdshell',1;

Step 5: Write up query

In the event you don’t have images this is what I used to load up a few sample files. I literally used the sample pictures that came preloaded with windows 7. Which are located in the C:\Users\Public\Pictures\Sample Pictures directory.

INSERT INTO devdb.dbo.Images ([Image],[FileName]) SELECT BulkColumn, 'Chrysanthemum.jpg' FROM OPENROWSET(BULK 'C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg',SINGLE_BLOB) AS x;
INSERT INTO devdb.dbo.Images ([Image],[FileName]) SELECT BulkColumn, 'Desert.jpg' FROM OPENROWSET(BULK 'C:\Users\Public\Pictures\Sample Pictures\Desert.jpg',SINGLE_BLOB) AS x;
INSERT INTO devdb.dbo.Images ([Image],[FileName]) SELECT BulkColumn, 'Hydrangeas.jpg' FROM OPENROWSET(BULK 'C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg',SINGLE_BLOB) AS x;
INSERT INTO devdb.dbo.Images ([Image],[FileName]) SELECT BulkColumn, 'Jellyfish.jpg' FROM OPENROWSET(BULK 'C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg',SINGLE_BLOB) AS x;
INSERT INTO devdb.dbo.Images ([Image],[FileName]) SELECT BulkColumn, 'Koala.jpg' FROM OPENROWSET(BULK 'C:\Users\Public\Pictures\Sample Pictures\Koala.jpg',SINGLE_BLOB) AS x;
INSERT INTO devdb.dbo.Images ([Image],[FileName]) SELECT BulkColumn, 'Lighthouse.jpg' FROM OPENROWSET(BULK 'C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg',SINGLE_BLOB) AS x;
INSERT INTO devdb.dbo.Images ([Image],[FileName]) SELECT BulkColumn, 'Penguins.jpg' FROM OPENROWSET(BULK 'C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg',SINGLE_BLOB) AS x;
INSERT INTO devdb.dbo.Images ([Image],[FileName]) SELECT BulkColumn, 'Tulips.jpg' FROM OPENROWSET(BULK 'C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg',SINGLE_BLOB) AS x;

Let’s take a look at the contents of the table.

SELECT id, Image, FileName FROM devdb.dbo.Images;

Step 6: Export images

Now that we know the table definition, adjusted the format file and enabled xp_cmdshell let’s extract some photos! We could run through this one-by-one but where’s the fun in that. I used a WHILE loop as opposed to a cursor to extract each photo by id.

DECLARE @cnt INT, @rcnt INT, @cmd NVARCHAR(150), @fname NVARCHAR(50), @filepath NVARCHAR(50), @inst NVARCHAR(50)

SELECT @rcnt = COUNT (*) FROM [devdb].[dbo].[Images]
SET @cnt = 1
SET @filepath = 'c:\temp\'
SET @inst = '(local)\DEV12'

WHILE @cnt < @rcnt
		SELECT @fname = FileName FROM devdb.dbo.Images WHERE (id = @cnt)
		SELECT @cmd = 'BCP "SELECT Image FROM [devdb].[dbo].[Images] WHERE (id = ' + CAST(@cnt AS VARCHAR) + ')" queryout "' + @filepath + @fname + '" -T -S ' + @inst + ' -f c:\temp\Images.fmt'
		--SELECT @cmd = 'BCP "SELECT Image FROM [devdb].[dbo].[Images] WHERE (id = ' + CAST(@cnt AS VARCHAR) + ')" queryout "c:\temp\' + @fname + '" -T -S (local)\DEV12 -f c:\temp\Images.fmt'
		EXEC xp_cmdshell @cmd
		SET @cnt = @cnt + 1


That’s pretty much it. Happy BCP’ing and exporting!

Extending Recent Files List in SSMS 2012

If you are like me you save most (if not all) of the scripts or queries that you create. It is just a huge time saver and with our aggressive work load we need all the time we can spare. In my case I deal with a significant volume of production deployments/promotions so I burn through scripts frequently.

On a few occasions (when I need it most of course) the file names often drift from memory so I sometimes look to my recent file list in SSMS to help jog my memory. Yes we have deployment request which I can easily reference but this post is about SQL Server Management Studio, so there. :-)

Back to the subject… by default SSMS 2012 will only display the last six but what if you want to extend that number to 10 or more?


I’d like to point out that Jugal Shah (blog | @imjugal) published an article that covers SSMS 2000, 2005, 2008 and 2008 R2. Here is the link to his article if you would like to read his post Change Setting for Recently Used Files in SQL Server Management Studio SSMS.

The process is pretty much identical with SSMS 2012 and you go about it like so…

Step 1: Tools >> Options


Step 2: Increase the value for: “items shown in recently used lists”


Step 3: Let’s Verify

The change is immediate, so there’s no need to close and reopen SSMS.