Advertisement

[x]
Attachment Details

Trouble with setting up Transaction/Stored Procedure

[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.4
Being relatively new to both SQL and the concept of transaction I am having some troubles with the following scenario:
 
I have a database named Event2 that contains two tables.

The first table Events is where the information for each event is stored.  The table consists of:

ID(primary key)      int                  
Date                  smalldatetime      
Time                  varchar(50)      
Location            varchar(50)      
Number            int      
Name                  varchar(50)      
Description            varchar(50)

The second table Attendees is where in the information is stored for each person who signs up for an event.  The table consists of:

PersonID(primary key)      int      
FirstName                  varchar(50)      
LastName                  varchar(50)      
PhoneNumber                  varchar(50)      
Email                        varchar(50)      
Event                        int

Each row in the Attendees table is associated with an event that is listed in the Events table. The value in the Event column of the Attendees table displays the ID of the event it is associated with in the Events table.

Furthermore, it is important to note that the Number column in the Events table displays the number of rows that exist (were created) in the Attendees table for that specific event (each row in the Attendees table represents a seat at an event).

It may also be interesting to note that every column in each of the talbles allows nulls (with the exception of the primary keys).

This brings me to my problem.  I am working on a stored procedure that will make any changes an administrator makes to a specific event (including the total number of seats at the event, keeping mind to never delete a person/seat who has already signed up for an event).

Here is what I have so far  (it doesnt work).


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[UpdateEvent2]
@Date smalldatetime,
@Time varchar (50),
@Name varchar (50),
@Description varchar (50),
@WebTotalSeats varchar (50),
@ID int

AS

BEGIN TRANSACTION

UPDATE Events
SET
Date = @Date,
Time = @Time,
Name = @Name,
Description = @Description
WHERE ID = @ID

SELECT Number AS DBTotalSeats FROM Events WHERE ID = @ID

IF @WebTotalSeats = DBTotalSeats

COMMIT TRANSACTION

 


ELSE
IF @WebTotalSeats < DBTotalSeats

SELECT DBTotalSeats - @WebTotalSeats As DecreaseDifference

UPDATE Events  
SET
Number = @WebTotalSeats
WHERE ID = @ID

IF @@ROWCOUNT = 1

DELETE Top DecreaseDifference
FROM Attendees
WHERE Event = @ID AND FirstName IS NULL

IF @@ROWCOUNT <> DecreaseDifference
ROLLBACK TRANSACTION
ELSE
COMMIT TRANSACTION
END




ELSE
IF @WebTotalSeats > DBTotalSeats
SELECT @WebTotalSeats - DBTotalSeats As IncreaseDifference

UPDATE Events
SET
Number = @WebTotalSeats
WHERE ID = @ID

IF @@ROWCOUNT = 1

INSERT Top IncreaseDifference
INTO Attendees (Event)
VALUES (@WebTotalSeats)
 
IF @@ROWCOUNT <> IncreaseDifference
ROLLBACK TRANSACTION
ELSE
COMMIT TRANSACTION

Related Solutions
Related Solutions
 
Loading Advertisement...
 

Rank: Sage

Expert Comment by dqmq:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 

Rank: Sage

Expert Comment by dqmq:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 

Rank: Sage

Expert Comment by dqmq:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Author Comment by DanGettel:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Author Comment by DanGettel:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 

Rank: Sage

Expert Comment by dqmq:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Author Comment by DanGettel:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Author Comment by DanGettel:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 

Rank: Sage

Accepted Solution by dqmq:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
Loading Advertisement...
20080924-EE-VQP-41 / EE_QW_2_20070628