Stored Procedure to reliably update two tables.

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 25
Reputation: stockton is an unknown quantity at this point 
Solved Threads: 0
stockton stockton is offline Offline
Light Poster

Stored Procedure to reliably update two tables.

 
0
  #1
Jul 24th, 2008
I have written the following Stored Procedure in an attempt to update two tables in the same database reliably but unfortunately it is not too successful. I ocassionaly end up with only the BundlesIssued tables updated and nothing in TicketsIssued.
Please make suggestions on how I could make this stored procedure update both tables reliably.

  1.  
  2. ALTER PROCEDURE spIssueScannedTickets
  3. @iEventID int,
  4. @MemberNum nvarchar(12),
  5. @BatchSize int,
  6. @FirstNumber nvarchar(12),
  7. @LastNumber nvarchar(12),
  8. @SlotsBalance int,
  9. @TableBalance int,
  10. @BonusBalance int,
  11. @UserID int
  12. AS
  13. DECLARE @Result int
  14.  
  15. -- Declare variables used in error checking.
  16. DECLARE @error_var int, @rowcount_var int
  17.  
  18.  
  19. SET @Result = 0
  20. BEGIN TRANSACTION
  21. INSERT INTO BundlesIssued (MemberNumber, EventID, BundleSize, FirstNumber, LastNumber, DateIssued, UserID, Invalid)
  22. VALUES (@MemberNum, @iEventID, @BatchSize, @FirstNumber, @LastNumber, GETDATE(), @UserID , 0x00)
  23. IF (@SlotsBalance>0)
  24.  
  25. -- Save the @@ERROR and @@ROWCOUNT values in local
  26. -- variables before they are cleared.
  27. SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT
  28. -- check if an error occured and if the expected number of records were affected
  29. IF @error_var <> 0 OR @rowcount_var <> 1
  30. BEGIN
  31. ROLLBACK
  32. -- PRINT "Warning: Error on Insert 1"
  33. RETURN(-1)
  34. END
  35.  
  36. INSERT INTO TicketsIssued (Date, EventID, MemberNumber, EarnType, NumTicketsIssued, Turnover, UserID)
  37. VALUES (GETDATE(),@iEventID,@MemberNum, 'Slots', @SlotsBalance, 0, @UserID)
  38. IF (@TableBalance>0)
  39.  
  40. -- Save the @@ERROR and @@ROWCOUNT values in local
  41. -- variables before they are cleared.
  42. SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT
  43. -- check if an error occured and if the expected number of records were affected
  44. IF @error_var <> 0 OR @rowcount_var <> 1
  45. BEGIN
  46. ROLLBACK
  47. -- PRINT "Warning: Error on Insert 2"
  48. RETURN(-2)
  49. END
  50.  
  51. INSERT INTO TicketsIssued (Date, EventID, MemberNumber, EarnType, NumTicketsIssued, Turnover, UserID)
  52. VALUES (GETDATE(),@iEventID,@MemberNum, 'Tables', @TableBalance, 0, @UserID)
  53. IF (@BonusBalance>0)
  54. INSERT INTO TicketsIssued (Date, EventID, MemberNumber, EarnType, NumTicketsIssued, Turnover, UserID)
  55. VALUES (GETDATE(),@iEventID,@MemberNum, 'Bonus', @BonusBalance, 0, @UserID)
  56.  
  57. -- Save the @@ERROR and @@ROWCOUNT values in local
  58. -- variables before they are cleared.
  59. SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT
  60. -- check if an error occured and if the expected number of records were affected
  61. IF @error_var <> 0 OR @rowcount_var <> 1
  62. BEGIN
  63. ROLLBACK
  64. -- PRINT "Warning: Error on Insert 3"
  65. RETURN(-3)
  66. END
  67.  
  68.  
  69. COMMIT TRANSACTION
  70. SET @Result = @BatchSize
  71. RETURN @Result
  72. GO
  73.  
  74. -- The function will return your batchSize (that was passed to it, or a Negative number if an error occured
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Stored Procedure to reliably update two tables.

 
0
  #2
Jul 24th, 2008
hi,

how do you access the database? If by ODBC, you should set autocommit OFF. Unfortunately, ODBC's default is ON!

krs,
tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 25
Reputation: stockton is an unknown quantity at this point 
Solved Threads: 0
stockton stockton is offline Offline
Light Poster

Re: Stored Procedure to reliably update two tables.

 
0
  #3
Jul 25th, 2008
I use the following PHP to do the call
  1. /*
  2.  * Call spIssueScannedTickets stored procedure to update BundleIssued and TicketIssued
  3.  * required parameters are:-
  4.  * MemberNumber, EventID, BundleSize, FirstNumber, LastNumber, UserID, Invalid and
  5.  * SlotsBalance & TableBalance & BonusBalance
  6.  * ie the amount of tickets to issue for Slots, Tables & Bonus
  7.  */
  8.  
  9. $Result = 0;
  10. $proc = mssql_init("spIssueScannedTickets",$link);
  11. mssql_bind($proc, "@MemberNum", $MemberNum, SQLVARCHAR);
  12. mssql_bind($proc, "@iEventID", $EventID, SQLINT2);
  13. mssql_bind($proc, "@BatchSize", $SizeOfBundle, SQLINT2);
  14. mssql_bind($proc, "@FirstNumber", $FirstNumber, SQLVARCHAR);
  15. mssql_bind($proc, "@LastNumber", $LastNumber, SQLVARCHAR);
  16. mssql_bind($proc, "@UserID", $_SESSION['UID'], SQLINT2);
  17. mssql_bind($proc, "@SlotsBalance", $SlotsToIssue, SQLINT2);
  18. mssql_bind($proc, "@TableBalance", $TableToIssue, SQLINT2);
  19. mssql_bind($proc, "@BonusBalance", $BonusToIssue, SQLINT2);
  20. mssql_bind($proc, "RETVAL", $Result, SQLINT2);
  21. mssql_execute($proc);
  22. mssql_free_statement($proc);
and to link to the database I use
  1. <?php
  2. FUNCTION DBConnect() {
  3. global $dbservertype, $servername, $dbusername, $dbpassword, $dbname, $link;
  4. // echo "<br>In DBConnect";
  5. $link = mssql_connect($servername, $dbusername, $dbpassword);
  6. IF (!$link) {
  7. echo "Error = " . mssql_get_last_message();
  8. sprintf($Message, "At %d in %s Could not connect to %s at %s as %s",__LINE__, __FILE__, $dbname, $servername, $dbusername);
  9. trigger_error(E_USER_ERROR, $Message);
  10. exit;
  11. }
  12.  
  13. IF (!(mssql_select_db($dbname, $link))) {
  14. echo "Error = " . mssql_get_last_message();
  15. sprintf($Message, "At %d in %s Could not select %s at %s as %s",__LINE__, __FILE__, $dbname, $servername, $dbusername);
  16. trigger_error(E_USER_ERROR, $Message);
  17. exit;
  18. }
  19. RETURN $link;
  20. }
  21. ?>
Last edited by peter_budo; Jul 25th, 2008 at 9:18 am. Reason: Just correcting closing tag
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Stored Procedure to reliably update two tables.

 
0
  #4
Jul 25th, 2008
Hello,

php default's is auto commit ON. Therefore, you first step should always be switching it OFF, if you want to do serious database programming. With auto commit ON you will never be able to update two tables correctly, if they correlate, apart from being fond of always playing Russian roulette.

krs,
tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC