Create Function Varchar Returns Int As Begin Declare Float Select
Create Function Varchar Returns Int As Begin Declare Float Select
AS Begin
…….
End
CREATE FUNTION function_name ( [@parameter_name parameter_data_type] )
RETURNS table
AS
return [select command]
create function
GetGPA(@studentID varchar(10))
returns int
as begin
declare @gpa float
select
@gpa=sum(r.mark*s.credits)/sum(s
.credits)
from result r, subject s
where r.subjectID=s.ID
and r.studentID=@studentID
and r.times >= all (select
r1.times
from result r1 where
r1.studentID= r.studentID and
r1.subjectID= r.subjectID)
return @gpa
end
declare @a float
set @a=dbo.GetGPA('HV000001 ')
print @a
2. Use the above function to write the query for showing a table consisting of
2 columns StudentID and Average Score
3. Use the above function to write the query for showing the list of students whose GPAs
are greater than 7.
4. Add the column named Average Score to the table of student. Then, use the above function to
write the query for updating the average scores of all students.
Alter table student
Add Average_Score float
Update student set Average_Score=dbo. getGPA(ID)
https://www.w3schools.com/sql/sql_ref_sqlserver.asp