Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Tricky SP
Message
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Title:
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01326778
Message ID:
01326835
Views:
13
This message has been marked as a message which has helped to the initial question of the thread.
>Hi everybody,
>
>I'm working on an SP and there is some problem I'm trying to figure out the best way to solve.
>
>I'm writing PersonUpdate procedure right now.
>
>Depending on the Person Type this procedure tries to update several tables. I want to wrap the update in the transaction.
>
>All person types update People table with the same code.
>
>The way I coded it right now is this (pseudo-code)
>
>BEGIN TRY
>
>if type = one type
>   begin transaction
>     Update People
>     Update Other tables
>   commit transaction
>else if different type
>   begin transaction
>     Update People
>     Update Other tables
>   commit transaction
>etc.
>END TRY
>BEGIN TRY
>   General logic for failure, rollback transaction
>end try
>
>
>I'm wondering if there is a way to somehow move Update People logic to do it only once.
>
>Do you see a way to re-organize this procedure?
>
>Thanks again.
BEGIN TRY
begin transaction
    Update People

if type = one type
   Update Other tables
else if different type
   Update Other tables

--- COMMIT is outside any IFs
commit transaction
etc.
END TRY

BEGIN CATCH -- I think it should be CATCH here, not TRY 
   General logic for failure, rollback transaction
END CATCH
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform