I wrote this list of advice a couple of years ago, a few weeks after I was assigned the task of maintaining the kernel of a corporate information system written in TransactSql. Unfortunately, all the advice is based on real experience... -- DmitryJemerov
Not that modular code is always a big win in TransactSQL: Stored procedures and triggers can't be nested deeper than 16 levels. In practice, we found that Sybase 4.9x versions failed at somewhere around 12 or 13 levels.
I worked on a business system a few years ago that did practically all business logic in triggers and stored procedures. To ship an order, for instance, you'd write the general ledger transaction for it which, as a side effect (read: trigger) would update inventory, change business objects, etc. We ran into all the hard limits on stored procedure size and nesting depth.
The project failed (of course! ;-) but not for technical reasons (really!). Seems the project never really made any business sense, and eventually they realized that they didn't have sufficient money to build the system anyway. -- JeffGrigg
Doing everything in the database and exploiting it to the full is one way to avoid DatabaseImpedanceMismatch. Usually wholehearted avoidance is in the other direction and the database just gets used as a dumb store of marshalled objects.
Actually, we never encountered the nesting level problem. That system didn't use any triggers at all (all the business and other logic was coded in stored procedures), and I would say that we never had more than about 5 levels of nesting. We had a class hierarchy with a system of event handlers, and everything was done by calling several event handlers in a row. Many of the event handlers didn't call any other procedures at all.
The project did fail, and partly because of technical reasons. But the main problem was not the nesting level: it was the scalability. Basically, all the processing of each user's action was done in one big transaction that sometimes accessed half of the tables in the database and took many seconds to complete. Imagine how well it worked when there were 10 users.
Never delete anything from the code. Some day it may turn out to be useful! If a part of a procedure is no longer necessary - comment it, if an entire procedure is not necessary - leave it there.
In an environment where there is no source control, this is actually a Good Idea. Sadly, such environments are all too common even today (see MicrosoftAccess for an example).
"The project failed (of course! ;-). "
Hmmm. I was involved in writing an integrated trade feed/workflow tool that worked like this. It was fantastic, bullet-proof reliable and ran virtually without support (maybe 5 min a week?). The users loved it. The last I heard over 2m trades had gone through it.
See also: MisuseOfSql