-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddRacer.sql
51 lines (49 loc) · 3.51 KB
/
AddRacer.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
USE [eRaceClub]
GO
/****** Object: StoredProcedure [dbo].[AddRacer] Script Date: 12/02/2007 01:21:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[AddRacer](@raceID int, @carid int,@memberid int )
as
Begin
declare @RaceDate datetime
set @RaceDate = (Select RaceDate from Race where raceid = 1--@raceID)
if getDate() < @RaceDate
Begin
if not exists(select memberId from member where memberId = @memberid)
begin
RaisError('Member is not active. Add denied.',16,1)
end
else
if not exists(select carId from car where carId = @carid)
begin
RaisError('Car is not active. Add denied.',16,1)
end
else
begin
declare @M_certificate char(1)
Select @M_certificate = CertificationLevel from Member where memberId = @memberid
if not exists(select carid from car where carId = @carid and State = 'Certified')
begin
RaisError('Car must be certified. Add denied.',16,1)
end
Else
begin
Insert into RaceDetail(RaceId,CarID,MemberID,Certification)
Values(@RaceID,@carid,@memberid,@M_certificate)
Select Race.NumberOfCars,RaceDetail.RaceID,Race.Run,RaceDetail.CarID,RaceDetail.MemberId 'MemberID',member.FirstName + ' '+ member.LastName 'Name',Member.CertificationLevel 'Cert', Place 'Finish',convert(varchar(17),Runtime-Race.RaceDate,108) as 'RunTime',Penalty,RaceDetail.Comment from RaceDetail
inner join Member on RaceDetail.memberId = Member.memberId
inner join Race on RaceDetail.RaceId = Race.RaceId
where RaceDetail.RaceId = @RaceID and RaceDetail.CarID = @carid and RaceDetail.memberid = @memberid
end
if @@error<>0 or @@rowcount<>1
RaisError('Could not insert record',16,1)
end
end
Else
Begin
RaisError('Can not add racer when race has begun',16,1)
End
end