4
4
CREATE PROC Info_About_Flights_Of_AirlinesCompany @code char (2 )
5
5
AS
6
6
SELECT
7
- a .name ,
8
- f .flight_number ,
7
+ a .name ,
8
+ f .flight_number ,
9
9
f .flight_date ,
10
10
s .dept_city ,
11
11
s .dept_time ,
27
27
s .flight_time ,
28
28
s .distance
29
29
FROM Airlines a
30
- INNER JOIN Flights f ON a .code = f .airline_code
31
- INNER JOIN Schedules s ON a .code = s .airline_code
30
+ INNER JOIN
31
+ Flights f ON a .code = f .airline_code
32
+ INNER JOIN
33
+ Schedules s ON a .code = s .airline_code
32
34
WHERE a .code = @code
33
35
GO
34
36
38
40
CREATE PROC InfoForAirlineCompanyFlights @flight_date date , @dept_country char (2 )
39
41
AS
40
42
SELECT
41
- a .name ,
43
+ a .name ,
42
44
f.*
43
45
FROM Schedules s
44
- INNER JOIN Flights f ON f .flight_number = s .flight_number
45
- INNER JOIN Airlines a ON a .code = s .airline_code
46
+ INNER JOIN
47
+ Flights f ON f .flight_number = s .flight_number
48
+ INNER JOIN
49
+ Airlines a ON a .code = s .airline_code
46
50
WHERE f .flight_date = @flight_date AND s .dept_country = @dept_country
47
51
GO
48
52
49
53
EXEC InfoForAirlineCompanyFlights ' 2012-05-23' , ' US'
50
54
GO
51
55
52
- CREATE FUNCTION GetCountOfReservations(@passenger_name varchar (100 ), @code char (2 ))
56
+ CREATE FUNCTION GetCountOfReservations(@customer_name varchar (100 ), @code char (2 ))
53
57
RETURNS integer
54
58
AS
55
59
BEGIN
56
60
DECLARE @countOfReservations integer ;
57
- SET @countOfReservations = (SELECT COUNT (b .customer_number )
58
- FROM Bookings b
59
- INNER JOIN Flights f ON b .airline_code = f .airline_code
61
+ SET @countOfReservations = (SELECT COUNT (b .booking_number )
62
+ FROM Bookings b , Flights f
60
63
WHERE b .airline_code = @code
61
- AND b .customer_name = @passenger_name
62
- AND YEAR (f .flight_date ) = 2012 )
64
+ AND b .customer_name = @customer_name
65
+ AND YEAR (f .flight_date ) = 2012
66
+ AND b .flight_number = f .flight_number )
63
67
RETURN @countOfReservations
64
68
END ;
65
69
GO
66
70
71
+ DROP FUNCTION dbo .GetCountOfReservations
72
+ GO
73
+
74
+ DROP PROCEDURE ResultSetProc
75
+ GO
76
+
77
+ EXEC ResultSetProc 1000
78
+ GO
79
+
67
80
CREATE PROC ResultSetProc @flight_number integer
68
81
AS
69
- DECLARE @first_set table (passenger_name varchar (100 ));
70
- INSERT INTO @first_set SELECT b .customer_name
71
- FROM Bookings b
72
- WHERE b .flight_number = @flight_number
73
- DECLARE @second_set table (passenger_name varchar (100 ), airline_name varchar (20 ),
74
- bookings_count integer );
75
- INSERT INTO @second_set SELECT
76
- b .customer_name ,
77
- a .name ,
78
- dbo .GetCountOfReservations ((SELECT fs .passenger_name FROM @first_set fs), a .code )
79
- FROM Bookings b
80
- INNER JOIN Airlines a ON b .airline_code = a .code
81
- WHERE b .flight_number = @flight_number
82
- AND dbo .GetCountOfReservations ((SELECT fs .passenger_name FROM @first_set fs), a .code ) >= 3
82
+ SELECT DISTINCT (b .customer_name )
83
+ FROM Bookings b
84
+ WHERE b .flight_number = @flight_number
83
85
84
- SELECT * FROM @second_set
86
+ SELECT DISTINCT (b .customer_name ), a .name , dbo .GetCountOfReservations (b .customer_name , a .code )
87
+ FROM Bookings b, Airlines a
88
+ WHERE b .flight_number = @flight_number AND b .airline_code = a .code
89
+ -- AND dbo.GetCountOfReservations(b.customer_number, a.code) >=3
85
90
GO
86
91
87
- EXEC ResultSetProc 1000
92
+ DROP PROCEDURE ResultSetProc
88
93
GO
94
+
95
+ EXEC ResultSetProc 1000 WITH RESULT SETS
96
+ (
97
+ (
98
+ customer_name varchar (100 )
99
+ ),
100
+ (
101
+ customer_name varchar (100 ),
102
+ airline_name varchar (20 ),
103
+ bookings_count integer
104
+ )
105
+ );
106
+
107
+ INSERT INTO dbo .Bookings VALUES (' AA' , 1000 , 203 , 2873 , ' 2012-03-20' , ' Irene Barth' , 1 );
108
+ INSERT INTO dbo .Bookings VALUES (' AA' , 1000 , 205 , 2873 , ' 2012-03-22' , ' Irene Barth' , 0 );
0 commit comments