punkish has asked for the wisdom of the Perl Monks concerning the following question:
I have a storedProc transaction in a SQL Server db that I am exec-ing via DBD::ODBC. All cool.
Now I want to do it all in my script. I rewrote the multiple sql statements in my script, ran it, got an error that prepare failed, scratched my head, Google-d, and discovered the following --
Turn Autocommit off, RaiseError on, create a separate statement handle for each statement, wrap it all in eval, run it, check for errors, commit if everything is copacetic.
eval { $sth_1->execute; $sth_2->execute; $sth_3->execute; }; if ($@) $dbh->rollback; } else { $dbh->commit; }
What a pain! I have almost 40 statements that make up the transaction. So, my question -- is there another way or is the above the only way? (and yes, I know I can use a storedProc -- I already was -- I just want to be able to do this in the script as well).
Many thanks for any and all advice.I should have elaborated more in my OP. The pain was simply having to rewrite the entire script as separate statements. Some of the statements update or grab values that are then used in subsequent statements. Sample SQL code below ---
update: This goes on for 40 or 50 statements.ALTER PROCEDURE sp @foo VARCHAR(250), @bar VARCHAR(250) AS BEGIN TRAN DECLARE @baz NUMERIC, @quux NUMERIC SELECT @baz = col FROM table1 WHERE condition SELECT @quux = col FROM table2 WHERE condition = @baz INSERT INTO table3 (col1, col2, col3) VALUES (@foo, @bar, @quux) UPDATE someother nonsense COMMIT TRAN
I spent a good while getting all my SQL ducks in order getting it so it worked properly. Now, I have to do this in the Perl script, so I have to now write these statements separately, run them one by one, grab values from them to run subsequent statements, check for errors, commit or rollback. I was hoping to just run the entire block of multiple statements as a single script. Hence, the pain.
|
|---|