Question : Calling procedure or function from a trigger

The application that I work on uses a complicated replication mechanism that combines triggers and flat file databases.  For the sake of this question assume that we have to use custom triggers for replication.  

Within the trigger code, there is a section of code that is used over and over again to clean up special characters.  I was considering putting this code into a procedure or function and re-using it.  Obviously easier to maintain and is more correct.  

My concern is since this would be called from a trigger, would I be better off from a performance perspective to leave the "cleaning" code in the trigger?

Example of the code
  CURRENTLY
    SET @MyVariable = Replace (@MyVariable, '"', '\x022')
    SET @MyVariable = Replace (@MyVariable, '''', '\x027')
    SET @MyVariable = Replace (@MyVariable, char(10), '\x0a')
    SET @MyVariable = Replace (@MyVariable, char(13), '\x0d')
  WOULD LIKE TO DO
    Set @MyVariable = dbo.Clean(@MyVariable)
    ( Something along these lines )

I am specifically interested in the impact on performance of calling a proc or func multiple times in a trigger.

Answer : Calling procedure or function from a trigger

For max performance, I would leave the code in the triggers.  You will incur a LOT of overhead moving this to a function.

Btw, you should also stack/imbed them for best performance, something like:

SET @MyVariable = Replace(Replace(Replace(Replace (@MyVariable, '"', '\x022'), '''', '\x027'), char(10), '\x0a'), char(13), '\x0d')
Random Solutions  
 
programming4us programming4us