-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNews2Go.sql
251 lines (225 loc) · 5.46 KB
/
News2Go.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
IST245
Scott Hetherington
Lab 3: News 2 Go!
Stored Procedures
*/
--
-- Drop existing procedures
--
drop proc AddCarrier
drop proc UpdateDistributor
drop proc DeleteDropSite
drop proc LookUpDistributorCarrier
drop proc NoPapers
drop proc LookUpCustomer
drop proc TransferRegion
--
-- Procedure definitions
--
GO
--
-- #1: AddCarrier
--
create proc AddCarrier(@FirstName varchar(30) = null,
@LastName varchar(30) = null,
@Phone char(10) = null)
as
if @FirstName is null or @LastName is null or @Phone is null
begin
RaisError('This procedure does not allow null parameters.', 16, 1)
end
else
begin
if exists (select FirstName, LastName from Carrier where FirstName = @FirstName and LastName = @LastName)
begin
RaisError('Carrier already exists.', 16, 1)
end
else
begin
insert into Carrier(FirstName, LastName, Phone)
values(@FirstName, @LastName, @Phone)
select * from Carrier where CarrierID = @@identity
if @@Error <> 0
begin
RaisError('Could not add new Carrier.', 16, 1)
end
end
end
return
GO
--
-- #2: UpdateDistributor
--
create proc UpdateDistributor(@DistributorID int = null, @FirstName varchar(40) = null,
@LastName varchar(40) = null, @Wage smallmoney = null, @PagerNumber char(10) = null)
as
if @DistributorID is null or @LastName is null or @FirstName is null or @Wage is null or @PagerNumber is null
begin
RaisError('This procedure does not allow null parameters.', 16, 1)
end
else
begin
if not exists (select * from Distributor where DistributorID = @DistributorID)
begin
RaisError('Record does not exist, update failed.', 16, 1)
end
else
begin
update Distributor
set LastName = @LastName, FirstName = @FirstName,
Wage = @Wage, PagerNumber = @PagerNumber
where DistributorID = @DistributorID
if @@Error <> 0
begin
RaisError('Could not update Distributor.', 16, 1)
end
end
end
return
GO
--
-- #3: DeleteDropSite
--
create proc DeleteDropSite(@DropSiteID int = null)
as
if @DropSiteID is null
begin
RaisError('Dropsite ID is a required parameter.', 16, 1)
end
else
begin
if not exists (select * from DropSite where DropSiteID = @DropSiteID)
begin
RaisError('That Dropsite does not exist. Delete operation failed.', 16, 1)
end
else
begin
if exists (select * from Route where DropSiteID = @DropSiteID)
begin
RaisError('Dropsite in use by a route. Cannot delete.', 16, 1)
end
else
begin
delete from DropSite where DropSiteID = @DropSiteID
if @@Error <> 0
begin
RaisError('Delete operation failed.', 16, 1)
end
end
end
end
return
GO
--
-- #4: LookUpDistributorCarrier
--
create proc LookUpDistributorCarrier(@DistributorID int = null)
as
if @DistributorID is null
begin
RaisError('Distributor ID is a required parameter.', 16, 1)
end
else
begin
if not exists (select DistributorID from Distributor where DistributorID = @DistributorID)
begin
RaisError('That Distributor does not exist.', 16, 1)
end
else
begin
select Distributor.FirstName + ' ' + Distributor.LastName 'Distributor Name',
RegionName 'Region Name',
RouteName 'Route Name',
Carrier.FirstName + ' ' + Carrier.LastName 'Carrier Name' from Carrier
inner join Route on Carrier.CarrierID = Route.CarrierID
inner join Region on Route.RegionID = Region.RegionID
inner join Distributor on Region.DistributorID = Distributor.DistributorID
where Distributor.DistributorID = @DistributorID
end
end
return
GO
--
-- #5: NoPapers
--
create proc NoPapers
as
select FirstName + ' ' + LastName 'Customer Name', PostalCode 'Postal Code' from Customer
left outer join CustomerPaper on CustomerPaper.CustomerID = Customer.CustomerID
where CustomerPaper.CustomerID is null
return
GO
--
-- #6: LookUpCustomer
--
create proc LookUpCustomer(@PartialLastName varchar(30) = null)
as
if @PartialLastName is null
begin
RaisError('Partial last name is a required parameter.', 16, 1)
end
else
begin
if not exists (select LastName from Customer where LastName like '%' + @PartialLastName + '%')
begin
RaisError('No customers matching that criteria found.', 16, 1)
end
else
begin
select FirstName + ' ' + LastName 'Customer Name', Address, City, Province, PostalCode, PrePaidTip from Customer
where LastName like '%' + @PartialLastName + '%'
end
end
return
GO
--
-- #7: TransferRegion
--
create proc TransferRegion(@RegionID int = null, @DistributorID int = null)
as
if @RegionID is null or @DistributorID is null
begin
RaisError('This procedure does not allow null parameters.', 16, 1)
end
else
begin
begin transaction
update Distributor
set Wage = Wage - 1.00
where DistributorID = (select DistributorID from Region where RegionID = @RegionID)
if @@Error <> 0
begin
RaisError('The Distributor wage update failed.', 16, 1)
Rollback transaction
end
else
begin
update Region
set DistributorID = @DistributorID
where RegionID = @RegionID
if @@Error <> 0
begin
RaisError('The DistributorID update failed.', 16, 1)
Rollback transaction
end
else
begin
update Distributor
set Wage = Wage + 1.00
where DistributorID = @DistributorID
if @@Error <> 0
begin
RaisError('The second wage update failed.', 16, 1)
Rollback transaction
end
else
begin
commit transaction
end
end
end
end
end
return
GO