Running Advanced SQL Stored Procedures from Excel

April 30, 2012 No comments »

A while back, I posted two articles that showed you how to run Stored Procedures from Excel.

In “Running a SQL Stored Procedure from Excel“, I showed you how to fire a simple SQL Stored Procedure that returns a simple dataset. In “Running a SQL Stored Procedure from Excel with Dynamic Parameters“, I showed you how to pass parameters to a SQL Stored Procedure so you can filter the returned dataset.

.

In both of those examples, the common denominator is that the Stored Procedure can only contain a select statement. For example, ‘Select * from Market Sales‘.

.

But in some cases, your Stored Procedures may need to contain advanced functionality such as; creating temp tables, deleting data, updating data, etc. In these cases, you can’t simply fire the Stored Procedure from Excel. You’ll need to adjust the Procedure so that the results will return to Excel.

.

The Cause of the Problem

When you create a new Stored Procedure in SQL Server, you will automatically get a line that reads SET NOCOUNT ON. This one line of code, put at the top of a stored procedure turns off the messages that SQL Server sends back to the client after each T-SQL statement is executed. This is intended to improve performance by suppressing unnecessary messages to the client.

.

However, this will also suppress the returning of data to Excel, because SET NOCOUNT ON tells SQL Server that there is no need for this information to be passed back to the client.

.

The Fix

In this example, procedure, I’m creating two Temp tables in the procedure then creating a select statement which returns data from the two created tables. In order to be able to return the data to Excel with this Stored Procedure, I will need to add a line at the end of the Procedure that reads SET NOCOUNT OFF.

.

.

By wrapping your Stored Procedures with SET NOCOUNT ON … SET NOCOUNT OFF, you can fire all kinds of Stored Procedures from Excel:

  • Stored Procedures that create and use Temp Tables
  • Stored Procedures that contain Delete and Update Statements
  • Stored Procedures that contain Truncate and Insert Statements

.

In short, you can use this trick in conjunction with the other tricks you learned (in the other articles mentioned above) to trigger virtually any SQL Stored Procedure directly from Excel!

Hidden Pivot Table Calculated Field Command

April 26, 2012 1 comment »

A few months back, I discovered a hidden Pivot Table command on the Home tab in Excel.

If you put your cursor in the Values/Data area of a Pivot Table, you can Go to the Home Tab and select Insert->Insert Calculated Field.

.

If you put your cursor in the Row or Column area of a Pivot Table, you can Go to the Home Tab and select Insert->Insert Calculated Item.

.

.

Ok…I know you’re thinking that this is no big deal. I felt the same way when I first noticed it. I thought it was simply interesting that it was there – nothing more. But I have to say that I use it all the time now that I know it’s there.

.

I think it’s because even if I’m working with a Pivot Table, I’m usually on the Home tab. It’s easier for me go to Insert ->Insert Calculated Field rather the alternative; switch to the PivotTable Tools contextual tab, click on Options, and find the calculated field/item options in the Calculations group.

.

For you hot-key fans out there, I know there is a hot key that I could use.

I hate taking my hand off the mouse; I avoid it wherever I can. Other than Ctrl+C and Ctrl+V, I don’t do hot keys. Unless I’m stuck with a Darth Vader Mouse.

Group Sheets by Color

April 23, 2012 2 comments »

Just recently, I encountered a workbook where the worksheet tabs were colored. The colors apparently had some meaning, indicating that each colored tab is somehow related to another tab. So of course, I wanted to sort the tabs by color. It goes without saying that doing this manually was out of the question; there were too many tabs, I was too lazy, and the hour was late. So VBA to the rescue!

.

This macro iterates through the sheets in the workbook, comparing the tab color index of current sheet to that of the previous one. If the previous sheet has the same color index number as the current sheet, then we move the current sheet before it.

.

By the time all the iterations are done, all sheets are grouped together based on their tab colors.


.

Visual Basic:
  1. Sub GroupSheetsByColor()
  2.    
  3.     Dim CurrentSheetIndex As Integer
  4.     Dim PrevSheetIndex As Integer
  5.  
  6. 'Set starting counts and start looping
  7.     For CurrentSheetIndex = 1 To Sheets.Count
  8.     For PrevSheetIndex = 1 To CurrentSheetIndex - 1
  9.  
  10. 'Check Current sheet against Previous sheet to see if they match
  11.     If Sheets(PrevSheetIndex).Tab.ColorIndex = _
  12.     Sheets(CurrentSheetIndex).Tab.ColorIndex Then
  13.  
  14. 'If so, move Current sheet before Previous
  15.     Sheets(PrevSheetIndex).Move _
  16.     Before:=Sheets(CurrentSheetIndex)
  17.     End If
  18.  
  19. 'Loop back around to iterate again
  20.     Next PrevSheetIndex
  21.     Next CurrentSheetIndex
  22.  
  23. End Sub

.

You won't use this macro every day, but it is one of those macros you can keep around for special occasions.

Like a fine bottle of wine, or bean dip.

 

 

Live Training Events in May

April 13, 2012 No comments »

Over the last few weeks, you've probably run into some advertising about Live Excel Training Events. These events are an alternative to learning from a blog, a website, or online courses.

.

Why do I like live training events? Well, I spend all day in a chair (not moving my ass an inch to the left or right for hours). And although I do interact with the Excel community online, it's not the same as being in the same room, interacting in person. Here are some of the benefits I see each time I put on a live training event.

  • Lots of new Techniques being taught by expert instructors.
  • Meet and network with other analysts.
  • Plenty of ideas bounced around about new ways of doing things.
  • Walk away with free books and working examples.
  • Lots of food, drink, and nerdy fun.

.

.

It just so happens that we have an upcoming event!

We, Dick Kusleika (Daily Dose of Excel) and I (DataPig Technologies), will be hosting our Excel Power Analyst BootCamp in Omaha on May 22nd and 23rd.

 

This two-day boot camp is designed for Excel Power Analysts who are looking to more effectively build and manage better data reporting mechanisms. During this workshop, you'll be introduced to a wide array of tips and techniques that will muscle up your skills in Data Crunching, Reporting, and Automation.

Going beyond simple Excel tricks, you will learn to:

  • Gain efficiencies with powerful data crunching and spreadsheet auditing tips
  • Go beyond basic analysis with advanced PivotTables techniques
  • Create powerful dashboards with interactive data modeling methods
  • Integrate Excel with external data sources (SQL Server, Access, SharePoint)
  • Introduce powerful new BI capabilities with Power Pivot for Excel 2010
  • Implement macro-charged reporting with VBA

Sign up before May 1st and save $200 off registration!

.

.

But Mike, I don't live in the United States!

If you're and international Excel Expert, you should check out Chandoo's Live Events May – June.

He's hosting a series of events on Advanced Dashboards in Excel throughout Australia.

.

"This 2 day MasterClass is aimed at managers, analysts, reporting professionals, executives in sales, marketing, customer service or anyone who wants to learn about Excel Dashboards."

Some of the topics you will learn in Chandoo's MasterClass:

  • Excel formulas for dashboards
  • Applying advanced conditional formatting for dashboards
  • Selecting right charts for any situation
  • Excel 2010 – sparklines, slicers
  • Customizing Excel charts

Visit Chandoo.org to get more details.

Delete Icons Copied from the Web

April 4, 2012 7 comments »

So there you are, copying data from a web page into Excel. But when you paste the data, you notice that a bunch of annoying clip art and icons are pasted along with it. What do you do?

  1. Delete them one by one
  2. Have the Temp for Hire delete them one by one
  3. Go into the bathroom stall and play Angry Birds for 30 minutes

.

I'll show you what I do when this happens to me.
» Read more: Delete Icons Copied from the Web

Fill the Empty Cells Left by Pivot Table Row Fields

April 2, 2012 4 comments »

It's not uncommon to turn pivot tables into hard data by doing a Copy-Past Special-Values on them. This not only leaves you with the values created by the pivot table, but also the pivot table's data structure. Unfortunately, as you can see in this screenshot, this is often not ideal, as the pivot table leaves behind a bunch of empty cells that will have to be filled in before you can use this hard table.

.

In this post, I'll show you two relatively easy ways to fix this issue:
» Read more: Fill the Empty Cells Left by Pivot Table Row Fields

Showing an Ampersand on Button in Access

March 14, 2012 1 comment »

When creating forms in Access, typing an ampersand in the caption of a button will result in a keyboard Alt Hot Key.

For instance, if I wanted a button in my Access form to be triggered with the Hot Key Alt+k, I just need to enter the caption with an ampersand in front of the letter K.
» Read more: Showing an Ampersand on Button in Access

Exclusive Confidential Details from the MVP Summit

February 29, 2012 5 comments »

I'm at the Microsoft MVP summit this week. We're seeing plenty of new and innovative enhancements that will make it into future versions of Microsoft Office and Microsoft SharePoint. It's all exciting!

We're under strict Non-Disclosure Agreements, so I can't share everything with you. However, I can give you a redacted account of what we saw.
» Read more: Exclusive Confidential Details from the MVP Summit

Traveling Tip for the Memory Challenged

February 26, 2012 6 comments »

I'm heading off to the Microsoft MVP Summit in Redmond Washington. Every year, Microsoft invites the cadre of 3000 or so MVP nerds to gather and discuss the new and exciting products they are working on. This year, I've decided to go in light of the buzz I've been hearing around the new version of Microsoft Office.

.

But before I can rub elbows with the powerfully nerdy, I've got to fly there. This has me thinking about a travel tip of mine - for the memory challenged.
» Read more: Traveling Tip for the Memory Challenged

Circular Reference in an Access Query

February 24, 2012 2 comments »

I was working in Access the other day, when I built a query with the expression you see here in this screenshot.

.

I'm passing the [Profile Number] field through the RIGHT function to pad it with 10 zeros.

When I try to run the query, I get this error telling me that a Circular Reference is caused by the alias I'm using. Hmm….I didn't even know you could get a Circular Reference in Access.
» Read more: Circular Reference in an Access Query