Tony Davis

Simple-Talk Editor
News, views and good brews

Not the right place

Published Monday, March 17, 2008 11:43 AM

Many orthodoxies, or 'truisms', exist in IT; rules of programming that emerge in the light of shared experience and are then passed on from programmer to programmer and instilled as "the right way" to tackle a certain problem. The difficulty is that many of these truisms persist long after advances in technology have rendered them obsolete.

 

There is one persistent truism that always provokes vigorous head nodding amongst developers, and that is the assertion that the database is "not the right place" for any data presentation logic. According to this rule, SQL Server has no place in the presentation or rendering of data; its role is only to provide the raw datasets that can then be used in procedural code to represent the data on the screen.

 

However, with the introduction of the XML data type, as well as non-size-restricted types such as varchar(max), has SQL Server 2005 tipped the balance of the argument? Perhaps, now, there are circumstances where TSQL can make a strong contribution to the presentation of data

 

A while back, Simple-Talk published the Cross-Tab Pivot-table workbench, which showed one of several ways of providing cross-tab reports using TSQL. In our example, a stored procedure, spDynamicHTMLCrossTab, produced the HTML code which could be rendered directly on the browser. One of our readers responded by showing how it could be done using XML.

 

While well-received, there was the usual nervousness expressed by some readers, about TSQL code producing HTML. Nobody nowadays would bat an eyelid in surprise if you used SQL code to produce XML, which would subsequently be translated into HTML via XSLT. Why then strain at a gnat after swallowing a camel like this?

 

Sometimes, there will be cases where the insertion of extra logic in a .NET layer doesn’t seem to add much: If, for example, you wish to represent a hierarchical directory on a browser, such as an organisational list, is there anything intrinsically wrong, or morally suspect, in generating an XHTML fragment directly from a stored procedure, which could then be rendered on a browser? After all, the database knows all the factors which determine how a hierarchy should be represented.

 

Of course, there still remains a line which the database programmer should not cross. The database must never impose any restrictions in the way that data is rendered. The mark-up should be used to delimit the parts of the data in order to show the nature of the data, and its logical structure, without constraining the application programmer in any way.

 

An interesting comment was made at the Denver Code Camp last week that the interface between the database and the application developer should be a contract. This should be drawn up as part of the planning of a project in the light of the special demands and requirements of each project and team, rather than relying solely on existing orthodoxies.

 

We support this proposal, becasue we think it would force developers to constantly challenge these orthodoxies to make sure they are still true.What do you think? We'd love to hear from you and the best entry, made as a comment to this blog (you'll need to be signed in), will receive a $50 Amazon gift voucher.

 

Thanks to everyone for the fantastic response to the previous "How to layout SQL Code" editorial. The winner of the Amazon voucher is Tore. However, I give special mention to Alex Kuznetsov, louisducnguyen and billweh, who made it a tough choice.

 

Cheers,


Tony.

Comments

 

php code and scripts » Blog Archive » Not the right place said:

March 17, 2008 6:19 AM
 

RobertChipperfield said:

Leaving aside the moral "correctness" or otherwise of these techniques, what about the scalability implications of this?

Getting your SQL Server to render a full-blown HTML document is obviously going to consume CPU cycles, just as doing it in C# (or whatever) will do. However, whilst I can quite easily attach a nice load-balanced farm of four web servers to one database server, and parallel the incoming requests quite easily, I think the same is much more challenging in SQL Server, certainly without getting into the nasties of replication, read-only servers with log shipping and so on.

Of course, there's a trade-off here: at the other extreme, throwing an entire table over the network so that your app server has to filter the rows it wants is clearly madness, but if your SQL Server is only pushing the same amount of data to an app server, why not save it some work and push the load on to the app server? (Windows licenses are cheaper than SQL Server licenses, too!)

I also suspect - and, mind you, have no figures to back this up - that an ASP.NET application will be able to do complex presentation layer rendering more quickly than any T-SQL code ever will. This doesn't apply to SQLCLR code, but by the time you're doing that, why are you forcing yourself into an architecture that must be contained on a single machine?

When my website becomes vastly popular and the existing server struggles to cope (yeah, right!), I'd rather just be able to add another commodity dual-core box rather than have to shell out for an Extortionately Expensive Extremely Large Single Box server because all the work's being done in one place.
March 18, 2008 8:42 AM
 

Phil Factor said:

Scalability issues are an interesting point. It is not quite as clearcut as you might imagine. A database only needs to break into a sweat when modifying data. Where results can be stored or 'cached' as columns in tables as strings or XML, they can be accessed via ASP.NET with remarkable speed, so that a fairly static page, which requires to be updated at intervals of less than a minute, will probably require the equivalent box-power to  pulling it from a file, maybe less.
The point where scalability will become a worry is where the data pertaining to an individual entity is being served. However, even with plain bread-n-butter replication, one might even be in a better position to do web-farming than with a 'traditional' approach.
Certainly, my own experiments have surprised me with the flexibility of an approach that thinks in terms of XHTML fragments stored in SQL Server and mostly generated directly by SQL Server, that are pulled by ASP.NET into the final page.
There are always going to be processes that have to be done in procedural code. However, I think Tony is just saying that there may be cases where procedural code could be best generated within SQL Server, not that ALL of it should be done in SQL Server, though it would be rather fun to try!
March 18, 2008 10:27 AM
 

raibeart said:

One of the databases that I have developed is used to control a dynamic web site. All of the information, down to the location of the graphics, displayed on the site is in the database. If the end user wants any formatting of the data, ie.e paragraphs, new lines, bold, underline, lists, etc. then the markup code has to be included in the data stored. The site is a .Net 2.0 site and works quite well using this methodology. User like the idea of a dynamic site that they can change as needed without me needing to "replace" static HTML pages.
March 24, 2008 3:50 PM
 

TadRichard said:

The compromise that we have generally agreed upon in our dev shop is that Tables always contain format independent data, and that any formatting (HTML code) is allowed only as a computed column in a View.

We also append "HTML" to the column names for those Views/columns that include HTML formatting.

So for instance, we often have a "PeopleHTML" View that includes a column "FullAddressHTML" as

((((((([Address1]+'<br />')+case when len([Address2])>(0) then [Address2]+'<br />' else '' end)+[City])+', ')+[State])+'  ')+[ZipCode])

Neither the DBAs nor the Web Developers are 100% happy -- which obviously means we've struck the right balance.  ;-)
March 27, 2008 5:37 PM
You need to sign in to comment on this blog

















<March 2008>
SuMoTuWeThFrSa
2425262728291
2345678
9101112131415
16171819202122
23242526272829
303112345
Using Powershell to Generate Table-Creation Scripts
 For all of us who learn best by trying out examples, Bob Sheldon produces a PowerShell script file for... Read more...

Configuring Exchange Server 2007 to Support Information Rights Management
 In Exchange Server 2007, Information Rights management is easy to set up once you have set up the... Read more...

SQL Response: The dim sum interview
 Richard Morris met David and Nigel of the SQL Response team, in a dim sum Restaurant in Cambridge. They... Read more...

Why This SQL Server DBA is Learning Powershell
 Ron describes how he decided to study Powershell as a single scripting system to automate all the... Read more...

Using Covering Indexes to Improve Query Performance
 Designers of database systems will often assume that the use of a clustered index is always the best... Read more...