2

Greeting, Recently I've started to work on an application, where 8 different modules are using the same table at some point in the workflow. This table have an Instead-Of trigger, which is 5,000 lines long (where first 500 and last 500 lines are common for all modules, and then each module has its own 500 lines of code). Since the number of modules are going to grow and I want to keep thing as clear (and separate) as possible, I was wondering is there some sort of best practice to split trigger into stored procedures, or should I leave it all in one place? P.S. Are there going to be any performance penalties for calling procedures from the trigger and passing 15+ parameters to them?

2
  • What's a module in SQL Server? You mean just different database objects or something?
    – tommy_o
    Jul 10, 2013 at 22:28
  • I'm sorry for the confusion. I referred to module as to application module, that consist of database objects, business logic, etc. Say it's a management system, where 8 different apps refer to one table.
    – CubicsRube
    Jul 12, 2013 at 11:41

2 Answers 2

3

Bearing in mind that the inserted and deleted pseudo-tables are only accessible from within trigger code, and that they can contain multiple rows, you're facing two choices:

  • Process the rows in inserted and deleted in a RBAR1 fashion, to be able to pass scalar parameters to the stored procedures, or,

  • Copy all of the data from inserted and deleted into table variables that are then passed to the procedures as appropriate.

I'd expect either approach to impose some2 performance overhead, just from the copying

That being said, it sounds like too much is happening inside the triggers themselves - does all of this code have to be part of the same transaction that performed the DML statement? If not, consider using some form of queue (a table of requests or Service Broker, say) in which to place information on work to perform, and then process the data later - if you use Service Broker, you could have it inspect a shared message and then send appropriate messages to dedicated endpoints for each of your modules, as appropriate.

1 Row By Agonizing Row - using either a cursor of something else to simulate one to access each row in turn - usually frowned upon in a Set-based language like SQL.

2 How much is impossible to know without getting into the specifics of your code and probably trying all possible approaches and measuring the result.

1
  • Sorry for late reply, and thank you for your answer! Regarding RBAR: First, I'm trying to avoid cursors since I heard it's a bad practice. Second, I'm trying to avoid bulk updates for this table and allow to process only one row at a time (so no need for any other technique). Regarding your Q: No, each module uses its own part of the code. I'm using GOTO to enter and leave sections. I've come to solution to split the trigger into procedures, even though it will be more difficult to maintain if some major changes occur. Just opening all the procedures in different tabs is a distress. Cheers :)
    – CubicsRube
    Jul 18, 2013 at 11:41
1

I don't think there is a meaningful performance penalty in this case.

Any way, it is bad practice to write it all inside the trigger (when it is 5000 lines long...). I think the main consideration is maintainability, which will be much better if you split it To several SPs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.