473,583 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stored Procedure to reliably update two tables.

6 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE spIssueScannedTickets
  2.         @iEventID int,
  3.         @MemberNum nvarchar(12),
  4.         @BatchSize int,
  5.         @FirstNumber nvarchar(12),
  6.         @LastNumber nvarchar(12),
  7.         @SlotsBalance int,
  8.         @TableBalance int,
  9.         @BonusBalance int,
  10.         @UserID int
  11. AS
  12. DECLARE @Result int
  13.  
  14. -- Declare variables used in error checking.
  15. DECLARE @error_var int, @rowcount_var int
  16.  
  17.  
  18. SET @Result = 0
  19. BEGIN TRANSACTION
  20. INSERT INTO BundlesIssued (MemberNumber, EventID, BundleSize, FirstNumber, LastNumber, DateIssued, UserID, Invalid)
  21.         VALUES (@MemberNum, @iEventID, @BatchSize, @FirstNumber, @LastNumber, GETDATE(), @UserID , 0x00)
  22. if  (@SlotsBalance>0)
  23.  
  24. -- Save the @@ERROR and @@ROWCOUNT values in local
  25. -- variables before they are cleared.
  26. SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT
  27. -- check if an error occured and if the expected number of records were affected
  28. IF @error_var <> 0 or @rowcount_var <> 1
  29. BEGIN
  30.    ROLLBACK
  31. --   PRINT "Warning: Error on Insert 1"
  32.    RETURN(-1)
  33. END
  34.  
  35. INSERT INTO TicketsIssued (Date, EventID, MemberNumber, EarnType, NumTicketsIssued, Turnover, UserID)
  36.          VALUES (GETDATE(),@iEventID,@MemberNum, 'Slots', @SlotsBalance, 0, @UserID)
  37. if  (@TableBalance>0)
  38.  
  39. -- Save the @@ERROR and @@ROWCOUNT values in local
  40. -- variables before they are cleared.
  41. SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT
  42. -- check if an error occured and if the expected number of records were affected
  43. IF @error_var <> 0 or @rowcount_var <> 1
  44. BEGIN
  45.    ROLLBACK
  46. --   PRINT "Warning: Error on Insert 2"
  47.    RETURN(-2)
  48. END
  49.  
  50. INSERT INTO TicketsIssued (Date, EventID, MemberNumber, EarnType, NumTicketsIssued, Turnover, UserID)
  51.          VALUES (GETDATE(),@iEventID,@MemberNum, 'Tables', @TableBalance, 0, @UserID)
  52. if  (@BonusBalance>0)
  53. INSERT INTO TicketsIssued (Date, EventID, MemberNumber, EarnType, NumTicketsIssued, Turnover, UserID)
  54.          VALUES (GETDATE(),@iEventID,@MemberNum, 'Bonus', @BonusBalance, 0, @UserID)
  55.  
  56. -- Save the @@ERROR and @@ROWCOUNT values in local
  57. -- variables before they are cleared.
  58. SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT
  59. -- check if an error occured and if the expected number of records were affected
  60. IF @error_var <> 0 or @rowcount_var <> 1
  61. BEGIN
  62.    ROLLBACK
  63. --   PRINT "Warning: Error on Insert 3"
  64.    RETURN(-3)
  65. END
  66.  
  67.  
  68. COMMIT TRANSACTION
  69. SET @Result = @BatchSize
  70. RETURN @Result
  71. GO
  72.  
  73. -- The function will return your batchSize (that was passed to it, or a Negative number if an error occured
  74.  
  75.  
Jul 24 '08 #1
3 2541
ck9663
2,878 Recognized Expert Specialist
One word: TRIGGERS

-- CK
Jul 24 '08 #2
arbcoll
2 New Member
After each insert you got IF clause:

if (@SlotsBalance> 0)
if (@TableBalance> 0)

etc...

I couldn't get the idea behind these?
Jul 24 '08 #3
stockton
6 New Member
After each insert you got IF clause:

if (@SlotsBalance> 0)
if (@TableBalance> 0)

etc...

I couldn't get the idea behind these?
Sorry Guys, These lines should have been before the insert & I have no idea why they are now appearing after the insert. The insert is dependant on that value being non zero.
Therefore the code should read
Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE spIssueScannedTickets
  2.  
  3.     @iEventID int,
  4.  
  5.     @MemberNum nvarchar(12),
  6.  
  7.     @BatchSize int,
  8.  
  9.     @FirstNumber nvarchar(12),
  10.  
  11.     @LastNumber nvarchar(12),
  12.  
  13.     @SlotsBalance int,
  14.  
  15.     @TableBalance int,
  16.  
  17.     @BonusBalance int,
  18.  
  19.     @UserID    int
  20.  
  21. AS
  22.  
  23. DECLARE @Result int
  24.  
  25.  
  26.  
  27. -- Declare variables used in error checking.
  28.  
  29. DECLARE @error_var int, @rowcount_var int
  30.  
  31.  
  32.  
  33.  
  34.  
  35. SET @Result = 0
  36.  
  37. BEGIN TRANSACTION
  38. INSERT INTO BundlesIssued (MemberNumber, EventID, BundleSize, FirstNumber, LastNumber, DateIssued, UserID, Invalid)
  39.  
  40.     VALUES (@MemberNum, @iEventID, @BatchSize, @FirstNumber, @LastNumber, GETDATE(), @UserID , 0x00)
  41.  
  42.  
  43.  
  44. -- Save the @@ERROR and @@ROWCOUNT values in local 
  45.  
  46. -- variables before they are cleared.
  47.  
  48. SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT
  49.  
  50. -- check if an error occured and if the expected number of records were affected
  51.  
  52. IF @error_var <> 0 or @rowcount_var <> 1 
  53.  
  54. BEGIN
  55.  
  56.    ROLLBACK
  57.  
  58. --   PRINT "Warning: Error on Insert 1"
  59.  
  60.    RETURN(-1)
  61.  
  62. END
  63.  
  64. if  (@SlotsBalance>0)
  65.  
  66. INSERT INTO TicketsIssued (Date, EventID, MemberNumber, EarnType, NumTicketsIssued, Turnover, UserID)
  67.      VALUES (GETDATE(),@iEventID,@MemberNum, 'Slots', @SlotsBalance, 0, @UserID)
  68.  
  69.  
  70.  
  71. -- Save the @@ERROR and @@ROWCOUNT values in local 
  72.  
  73. -- variables before they are cleared.
  74.  
  75. SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT
  76.  
  77. -- check if an error occured and if the expected number of records were affected
  78.  
  79. IF @error_var <> 0 or @rowcount_var <> 1 
  80.  
  81. BEGIN
  82.  
  83.    ROLLBACK
  84.  
  85. --   PRINT "Warning: Error on Insert 2"
  86.  
  87.    RETURN(-2)
  88.  
  89. END
  90.  
  91. if  (@TableBalance>0)
  92. INSERT INTO TicketsIssued (Date, EventID, MemberNumber, EarnType, NumTicketsIssued, Turnover, UserID)
  93.      VALUES (GETDATE(),@iEventID,@MemberNum, 'Tables', @TableBalance, 0, @UserID)
  94.  
  95. if  (@BonusBalance>0)
  96.  
  97. INSERT INTO TicketsIssued (Date, EventID, MemberNumber, EarnType, NumTicketsIssued, Turnover, UserID)
  98.      VALUES (GETDATE(),@iEventID,@MemberNum, 'Bonus', @BonusBalance, 0, @UserID)
  99.  
  100.  
  101.  
  102. -- Save the @@ERROR and @@ROWCOUNT values in local 
  103.  
  104. -- variables before they are cleared.
  105.  
  106. SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT
  107.  
  108. -- check if an error occured and if the expected number of records were affected
  109.  
  110. IF @error_var <> 0 or @rowcount_var <> 1 
  111.  
  112. BEGIN
  113.  
  114.    ROLLBACK
  115.  
  116. --   PRINT "Warning: Error on Insert 3"
  117.  
  118.    RETURN(-3)
  119.  
  120. END
  121.  
  122.  
  123.  
  124.  
  125.  
  126. COMMIT TRANSACTION
  127.  
  128. SET @Result = @BatchSize
  129.  
  130. RETURN @Result
  131. GO    
  132.  
  133.  
  134. -- The function will return your batchSize (that was passed to it, or a Negative number if an error occured
  135.  
  136.  
  137.  
Jul 24 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
6229
by: deprins | last post by:
Hello, I have wrote a stored procedure but its real slow. Its activated by a button on web page but its takes to long to process and the web server gives a timeout message after 5 minutes. Is there anyway to speed up this stored procedure? What am I doing wrong here? ...
12
8334
by: serge | last post by:
I have an SP that is big, huge, 700-800 lines. I am not an expert but I need to figure out every possible way that I can improve the performance speed of this SP. In the next couple of weeks I will work on preparing SQL statements that will create the tables, insert sample record and run the SP. I would hope people will look at my SP and...
4
13462
by: marc | last post by:
I've been developing a stored procedure that uses a user defined function in the query portion of the procedure. However, since the end product needs to allow for dynamic table names, the UDF will not work. I've been trying to get this to work with converting the UDF to a procedure, but I'm having no luck. Here is the background on what I'm...
2
3322
by: Eli | last post by:
Hi all We currently have a strange problem with calling a Stored Procedure (SQL Database) in our C# Project. The only error I get is "System error" which says a lot :) Background: We have several stored procedures to Insert and update datas in our SQL database. Some stored procedures are smaller (insert datas in only one table) and some...
9
5267
by: joun | last post by:
Hi all, i'm using this code to insert records into an Access table from asp.net, using a stored procedure, called qry_InsertData: PARAMETERS Long, Long, Text(20), Long, DateTime; INSERT INTO Table ( ID, Cod, CodArt, Q1, DataUscita ) VALUES (pID, pCod, pCod, pQ1, pDataUscita);
7
2214
by: Peter D.C. | last post by:
Hi I want to update data hold in several textbox controls on an asp.net form. But it seems like it is the old textbox values that is "re-updates" through a stored procedure who updates a SQL tabel. I know the SP works, because a smalldatetime-field in the database is changed. I've tried to disable viewstate on all textbox controls. Any...
2
11496
by: syntego | last post by:
We commonly use triggers to log changes to our main tables to historical log tables. In the trigger, we create a concatenated string of the old values by casting them as follows: CAST(O.MYDATE AS CHAR(30)) When directly updating date fields in the main table, the logged value gets saved in the format YYYY-MM-DD as expected.
0
2749
by: svgeorge | last post by:
I want to update several tables using one stored procedure. How can i do this I mean the syntax.etc. declaration etc. I know the basic syntax as below CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName> -- Add the parameters for the stored procedure here <@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1,...
5
4067
by: Bogdan | last post by:
Hi, I have a stored procedure that uses JOINs to return columns from multiple tables. I also have another stored proc that that takes a series of params and updates multiple tables. I used the framework to auto-generate a table adapter specifying both stored procs as Get/Fill and Update. The problem is that columns from the JOINed table...
0
7824
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8176
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8321
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7931
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5370
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1154
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.