943,948 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 4113
  • MS SQL RSS
Jul 24th, 2008
0

Stored Procedure to reliably update two tables.

Expand Post »
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.

MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 20
Solved Threads: 0
Light Poster
stockton is offline Offline
32 posts
since Jul 2008
Jul 24th, 2008
0

Re: Stored Procedure to reliably update two tables.

hi,

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

krs,
tesu
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008
Jul 25th, 2008
0

Re: Stored Procedure to reliably update two tables.

I use the following PHP to do the call
MS SQL Syntax (Toggle Plain Text)
  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
MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 20
Solved Threads: 0
Light Poster
stockton is offline Offline
32 posts
since Jul 2008
Jul 25th, 2008
0

Re: Stored Procedure to reliably update two tables.

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
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Help with sql to select fields with equal values
Next Thread in MS SQL Forum Timeline: Help with Database Query...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC