SQL For Programmers – New Question

tp_vol3_007 I got this question last week that I thought would be worth discussing on the blog.

I would like to create a table that lists contributions by date, amount, and designation. In some cases one gift was split two (or more) ways. For example, a contribution of $1000 was given, and $500 went to the scholarship fund and $500 to athletics. The gift date would be the same for both gifts, but they must be recorded separately in order to split the gift. In my new table, I want to list them in rows like this:
date| amount| designation1, designation2, designation3, etc.

Would it be best to use a cursor or a stored procedure. I’ve never done anything like this before, so I’m not sure where to begin.


Um… using cursors WOULD be in a stored procedure, right?  But I think we are getting ahead of ourselves.

When I get these kinds of questions, I always have to put together the hidden pieces.  So I’m sorry if some of what I cover isn’t part of the question, but it is at least part of the answer.

First, let’s take a look at how you would store this kind of data in a database.  You should have two tables.  The first would be the transaction information:

Field Description
ID Unique transaction ID, probably auto incrementing integer.
Date Date the gift was given

The second table would hold the split information

Field Description
ID Unique split ID, also probably auto incrementing integer
TranactionID Links the record to the transaction row (above)
Amount The amount of the split
Description Description of the split
   

 

I’m not sure if the person who originally asked me the question was meaning to place the transaction and split information all in the same row in the same table or not, but I’ve seen enough new developers try to do this to at least make me aware that this is a possibility.

The reason you don’t want to put it in there, even if you could pre-determine the maximum number of splits, is because at some point in the future, there is a very real likelihood that the number will grow.  My rule is, “if there is N of something, where N > 1, then there will be N + 1 of that same thing.”  Therefore, you always write your code to handle multiple items as soon as you know there will be more than one.  It takes no longer to code and will more likely than not save you time in the future.

For the very same reason, you don’t want to display it as though it all came out of one row, either.  You’d either need to dynamically create your display to show multiple columns as the maximum number grew, or you’d need to modify the code as it grew.  Neither is a very good solution.

So how would you display this information, then?

As a master detail report

Date   Total
           Amount    Description


Date Total
Amount Description
05/06/09 $100.00  
  $50.00 Fund One
  $50.00 Fund Two
     
05/07/09 $75.00  
  $75.00 Some place else

 

Store procedure or cursor?  You should be able to produce a report like this using two store procedures–one to get the list of transactions and another to get the list of splits for a specific transaction.


Other Related Items:

SQL Antipatterns: Avoiding the Pitfalls of Database Programming (Pragmatic Programmers)SQL Antipatterns: Avoiding the Pitfalls of Database Programming (Pragmatic Programmers)

Each chapter in this book helps you identify, explain, and correct a unique and dangerous antipattern. The four parts of the book group the antiâ... Read More >

Papirius Cursor Spares Fabius by Popular Request Stretched Canvas Poster PrintPapirius Cursor Spares Fabius by Popular Request Stretched Canvas Poster Print

Decorate your home or office with high quality wall décor. Papirius Cursor Spares Fabius by Popular Request is that perfect piece that matches your style, interests, and budget.

Microsoft Visual Studio Professional 2005 [OLD VERSION]Microsoft Visual Studio Professional 2005 [OLD VERSION]Microsoft Visual Studio Professional 2005 is one of the most significant developer tools available for building high-performance, multi-tier applicati... Read More >

Related Post

Comments are closed.

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor