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. -
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
-
-
3 2541 ck9663 2,878
Recognized Expert Specialist
After each insert you got IF clause:
if (@SlotsBalance> 0)
if (@TableBalance> 0)
etc...
I couldn't get the idea behind these?
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 -
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
-
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
...
|
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...
|
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...
|
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...
|
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);
| |
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...
|
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.
|
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,...
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |