Click here to Skip to main content
15,901,284 members
Articles / Programming Languages / SQL

Where to put the logic? SQL? Code?

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
19 May 2011CPOL3 min read 11.7K   3   1
The decision of where to put logic isn’t always clear cut.

SQL is a powerful language and is very good at what it was made for – querying sets of data. C#, Java, and other languages are also powerful – more powerful than SQL in many ways. For looping and doing procedural tasks, C# can process records hundreds of times faster than a SQL cursor, and do it in fewer lines of logic. SQL simply wasn’t made to do this type of processing. Instead, SQL was made for set-based processing where tables and result-sets are used and merged together based on conditions. In a future post, I’ll talk about cursors, why they’re painfully slow, and what to do about them.

Similarly, SQL is as far from the User Interface as you can get. As such, many UI-related tasks are generally not appropriate in the SQL logic. For example, if names should appear on a web data grid with the format '[first name],[last name][middle initial]', one option is to return data from the SQL statement in this format. However, an arguably more appropriate method is to simply return the name parts and have logic in the user interface format and the full name in the desired format.

One example where it is uncertain whether the logic should be in SQL was raised on the SQLTeam website here. In this scenario, the primary value [name] being displayed covers multiple rows in the UI, and some of the data fields should only show up for the first instance of the name. At first glance, this type of data hiding seems more like a UI-related issue than anything that should be in the SQL query. But, as is evident in the thread, what if the UI is locked down?

In my next post, I’ll discuss a SQL question and answer [that I provided] from SQLTeam in some depth. For this question, it is arguable this logic should be in the business logic layer. Another forum poster suggested as much, and in a way I agree. However, the decision of where to put logic isn’t always clear cut.

Some questions to ponder:

  1. What if, as was the case in the forum thread, the UI or business logic layer is locked down and the only way to change the display is to change the data loaded in?
  2. What if instead of calling a website, this report needs to display on 2-3 web applications, be made available via a Web Service, be pulled by a console application, etc. While in theory a common business logic component could be developed that all of these could reference, another [likely easier] option is to simply do the centralization at the database level in a Stored Procedure.
  3. What if the result needs to be referenced by some other piece of SQL logic? Maybe it can be transformed into a view.
  4. What if the result is destined for an Excel file being whipped up quickly for a user asking for an ad-hoc report? What if they ask for it again with a slight logic modification a few minutes after you have finished modifying the Excel file formatting? Wouldn’t it be easier just having the logic in the SQL in this case?
  5. What if the developer tasked to writing the logic is simply more adept at coming up with a SQL-based solution for the proposed need?

For me, if something can be done in a set-based manner, I’ll attempt to think of a clean SQL-based way to get the job done. If at all possible, anything that can be done in a set-based manner goes into SQL logic [and anything that requires procedural logic goes into code].

This article was originally posted at http://www.helpwithsql.com?p=67

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWhere... Pin
dmjm-h25-May-11 7:04
dmjm-h25-May-11 7:04 
You pose useful questions but the example scenarios, while certainly realistic, only place constraints on the theoretically correct solutions. If the constraint is you can't change anything except the database extract then the answer has to be change the database extract, oh and look for a new job. UI locked down? WTF?
The database is for storing data. Data extraction is for extracting data, in a form in which it can be used by diverse business processes. That means you extract the data into business entities and the business processes do with it what they will. If there be common business logic amongst multiple processes then it should be encapsulated.
I tend to see things in black and white. For me the question really is, if (political or cost or arbitrary) constraints prevent me from making changes where they should be made, what principles do I use to formulate plan B?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.