MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 24th, 2008, 12:31 AM
stockton stockton is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 13 stockton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 35 sec
Reputation Power: 0
Stored Procedure to reliably update two tables.

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.
Code:
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)

-- 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

if  (@SlotsBalance>0)
INSERT INTO TicketsIssued (Date, EventID, MemberNumber, EarnType, NumTicketsIssued, Turnover, UserID)
         VALUES (GETDATE(),@iEventID,@MemberNum, 'Slots', @SlotsBalance, 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 2"
   RETURN(-2)
END

if  (@TableBalance>0)
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

Last edited by stockton : July 24th, 2008 at 07:22 AM. Reason: I got the code wrong

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Stored Procedure to reliably update two tables.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT