DBI202_-_FA_2024_-_PE_-_s_-_6_2892.webp
B

DBI202_-_FA_2024_-_PE_-_s_-_6_2892.webp

Question 8:
Create a function named F1 which calculates and returns an integer number which is the number of distinct patients having appointments with doctors of a given department, where departmentID int is the input parameter of the function.
For example, when we call the F1 function in the following query for calculating the number of distinct patients having appointments with the two departments 'Cardiology' and 'Pediatrics', the results must be shown as in the following figure:
select DepartmentID, Name, dbo. F1(DepartmentID) as NumberOfPatients
from Departments
where Departments.Name in ('Cardiology', 'Pediatrics')
DepartmentID Name NumberOfPatients
1
Cardiology 37
2 3
Pediatrics 32
Picture 8.1
1
Question 9:
Create a trigger named insertDoctor for the INSERT statement on the "Doctors" table so that when we execute an insert statement to add one or more rows to the "Doctors" table, if any of the inserted rows have the DepartmentID as null, the system will automatically set the DepartmentID of those rows to the DepartmentID of the department named 'Cardiology'.
For example, if we execute the following statements to insert two doctors (one with DepartmentID=NULL and the other with DepartmentID=5) and then select these two doctors,we can see that the DepartmentID of the doctor with DepartmentID=null is set to the id of the department named 'Cardiology' as shown in the following figure:
insert into Doctors (DoctorID, FirstName, LastName, PhoneNumber, Email, DepartmentID)values (70,'Van Chien', 'Nguyen', '0928367812', '[email protected]', null),(71, 'Thi Binh', 'Nguyen', '0989278127', '[email protected]',5)
select * from Doctors where DoctorID in (70,71)
DoctorID
70
FirstName
LastName
Phone Number Email
DepartmentID
Van Chien
Nguyen
0928367812
1
[email protected] 1
+ 100%
Close
Chưa có bình luận nào.

Thông tin

Category
DBI202
Thêm bởi
banana_milk
Ngày thêm
Lượt xem
485
Lượt bình luận
0
Rating
0.00 star(s) 0 đánh giá

Image metadata

Filename
DBI202_-_FA_2024_-_PE_-_s_-_6_2892.webp
File size
325.5 KB
Dimensions
1920px x 1008px

Share this media

Back
Bên trên Bottom