views:

84

answers:

3

My primary concern is with SQL Server 2005... I went through many website and each tells something different.

What are the scenarios that are good / ok to use.. For example does it hurts to even set variable values inside IF or only if I run a query. Supposing my SPs is building a dynamic SQL based of several conditions in Input Parameters, do I need to rethink about the query... What about a SP that runs different query based on whether some record exists in the table. etc.. etc.. My question is not just limited to these scenarios... I'm looking for a little more generalised answer so that I can improve my future SPs

In essense... Which statements are good to use in Branching conditions / Loops, which is bad and which is Okay.

+5  A: 

Generally... Avoid procedural code in your database, and stick to queries. That gives the Query Optimizer the chance to do its job much better.

The exceptions would be code that is designed to do many things, rather than making a result-set, and when a query would need to join rows exponentially to get a result.

Rob Farley
+1 1 for recommendation to avoid code in the db. I haven't got a clue what y man by "join rows exponentially" tho'....can you explain?
Roland Bouman
I think he might be referring to the problem of finding the closure of a relation.
Stephen C
For example, if you're trying to create a running total (until the OVER clause is improved), you need to join each row to all the previous ones. With just 6 rows, this is 21 joins, but as the number of rows increases, the number of rows increases in the order of O^2. So these queries don't scale.
Rob Farley
I would love to do it... But unfortunately my team mates thinks that, having some procedural code in SP makes the program (ASP.NET) easier to maintain, as we need not have to redeploy the code, everytime something small needs to be changed. We can just do that in SP and ASP.NET implements the change immediately... What do you suggest...
The King
Do NOT see having code in stored procedures as a shortcut to the software development lifecycle. You should use proper testing systems and deployment mechanisms. You can have procedural code in the database where appropriate, but do NOT use it to avoid the SDLC.
Rob Farley
+1  A: 

It is very hard to answer this question if you don't provide any code. No language construct is Good/Bad/Okay by itself, its what you want to achieve and how well that can be expressed with those constructs.

Roland Bouman
A: 

There's no definitive answer as it really depends on the situation.

In general, I think it's best to keep the logic within a sproc as simple and set-based as possible. Making it too complicated with multiple nested IF conditions for example, may complicate it for the query optimiser meaning it can't create a good execution plan suitable for all paths through the sproc. For example, the first time the sproc is run, it takes path A through the logic and the execution plan reflects this. The next time it runs with different parameters, it takes path B through but resuses the original execution plan which is not optimal for this second path. One solution to this is to break the load into separate stored procedures to call depending on the path being followed - this allows that sub sproc to be optimised and execution plan cached independently.

Loops can be the only viable option, but in general I'd try to not use them - always try to do things in a set-based fashion if it is possible.

AdaTheDev