| | |
Stored Procedure to reliably update two tables.
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 25
Reputation:
Solved Threads: 0
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.
Please make suggestions on how I could make this stored procedure update both tables reliably.
MS SQL Syntax (Toggle Plain Text)
ALTER PROCEDURE spIssueScannedTickets @iEventID int, @MemberNum nvarchar(12), @BatchSize int, @FirstNumber nvarchar(12), @LastNumber nvarchar(12), @SlotsBalance int, @TableBalance int, @BonusBalance int, @UserID int AS DECLARE @Result int -- Declare variables used in error checking. DECLARE @error_var int, @rowcount_var int SET @Result = 0 BEGIN TRANSACTION INSERT INTO BundlesIssued (MemberNumber, EventID, BundleSize, FirstNumber, LastNumber, DateIssued, UserID, Invalid) VALUES (@MemberNum, @iEventID, @BatchSize, @FirstNumber, @LastNumber, GETDATE(), @UserID , 0x00) IF (@SlotsBalance>0) -- Save the @@ERROR and @@ROWCOUNT values in local -- variables before they are cleared. SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT -- check if an error occured and if the expected number of records were affected IF @error_var <> 0 OR @rowcount_var <> 1 BEGIN ROLLBACK -- PRINT "Warning: Error on Insert 1" RETURN(-1) END INSERT INTO TicketsIssued (Date, EventID, MemberNumber, EarnType, NumTicketsIssued, Turnover, UserID) VALUES (GETDATE(),@iEventID,@MemberNum, 'Slots', @SlotsBalance, 0, @UserID) IF (@TableBalance>0) -- Save the @@ERROR and @@ROWCOUNT values in local -- variables before they are cleared. SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT -- check if an error occured and if the expected number of records were affected IF @error_var <> 0 OR @rowcount_var <> 1 BEGIN ROLLBACK -- PRINT "Warning: Error on Insert 2" RETURN(-2) END INSERT INTO TicketsIssued (Date, EventID, MemberNumber, EarnType, NumTicketsIssued, Turnover, UserID) VALUES (GETDATE(),@iEventID,@MemberNum, 'Tables', @TableBalance, 0, @UserID) IF (@BonusBalance>0) INSERT INTO TicketsIssued (Date, EventID, MemberNumber, EarnType, NumTicketsIssued, Turnover, UserID) VALUES (GETDATE(),@iEventID,@MemberNum, 'Bonus', @BonusBalance, 0, @UserID) -- Save the @@ERROR and @@ROWCOUNT values in local -- variables before they are cleared. SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT -- check if an error occured and if the expected number of records were affected IF @error_var <> 0 OR @rowcount_var <> 1 BEGIN ROLLBACK -- PRINT "Warning: Error on Insert 3" RETURN(-3) END COMMIT TRANSACTION SET @Result = @BatchSize RETURN @Result GO -- The function will return your batchSize (that was passed to it, or a Negative number if an error occured
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
hi,
how do you access the database? If by ODBC, you should set autocommit OFF. Unfortunately, ODBC's default is ON!
krs,
tesu
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.
•
•
Join Date: Jul 2008
Posts: 25
Reputation:
Solved Threads: 0
I use the following PHP to do the call
and to link to the database I use
MS SQL Syntax (Toggle Plain Text)
/* * Call spIssueScannedTickets stored procedure to update BundleIssued and TicketIssued * required parameters are:- * MemberNumber, EventID, BundleSize, FirstNumber, LastNumber, UserID, Invalid and * SlotsBalance & TableBalance & BonusBalance * ie the amount of tickets to issue for Slots, Tables & Bonus */ $Result = 0; $proc = mssql_init("spIssueScannedTickets",$link); mssql_bind($proc, "@MemberNum", $MemberNum, SQLVARCHAR); mssql_bind($proc, "@iEventID", $EventID, SQLINT2); mssql_bind($proc, "@BatchSize", $SizeOfBundle, SQLINT2); mssql_bind($proc, "@FirstNumber", $FirstNumber, SQLVARCHAR); mssql_bind($proc, "@LastNumber", $LastNumber, SQLVARCHAR); mssql_bind($proc, "@UserID", $_SESSION['UID'], SQLINT2); mssql_bind($proc, "@SlotsBalance", $SlotsToIssue, SQLINT2); mssql_bind($proc, "@TableBalance", $TableToIssue, SQLINT2); mssql_bind($proc, "@BonusBalance", $BonusToIssue, SQLINT2); mssql_bind($proc, "RETVAL", $Result, SQLINT2); mssql_execute($proc); mssql_free_statement($proc);
MS SQL Syntax (Toggle Plain Text)
<?php FUNCTION DBConnect() { global $dbservertype, $servername, $dbusername, $dbpassword, $dbname, $link; // echo "<br>In DBConnect"; $link = mssql_connect($servername, $dbusername, $dbpassword); IF (!$link) { echo "Error = " . mssql_get_last_message(); sprintf($Message, "At %d in %s Could not connect to %s at %s as %s",__LINE__, __FILE__, $dbname, $servername, $dbusername); trigger_error(E_USER_ERROR, $Message); exit; } IF (!(mssql_select_db($dbname, $link))) { echo "Error = " . mssql_get_last_message(); sprintf($Message, "At %d in %s Could not select %s at %s as %s",__LINE__, __FILE__, $dbname, $servername, $dbusername); trigger_error(E_USER_ERROR, $Message); exit; } RETURN $link; } ?>
Last edited by peter_budo; Jul 25th, 2008 at 9:18 am. Reason: Just correcting closing tag
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
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
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.
![]() |
Other Threads in the MS SQL Forum
- Previous Thread: Help with sql to select fields with equal values
- Next Thread: Help with Database Query...
| Thread Tools | Search this Thread |





