0% found this document useful (0 votes)
16 views3 pages

Create Function Varchar Returns Int As Begin Declare Float Select

This document discusses SQL functions in 3 parts. It begins by explaining how to create SQL functions that return scalar values or tables. It then provides 5 examples of functions - one to return average GPA for a student, and others to select student data using the GPA function. It shows how to use functions to select, update and return data in tables. The document demonstrates how to create and use SQL functions to encapsulate reusable code and perform queries on tables.

Uploaded by

HuyCường
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Create Function Varchar Returns Int As Begin Declare Float Select

This document discusses SQL functions in 3 parts. It begins by explaining how to create SQL functions that return scalar values or tables. It then provides 5 examples of functions - one to return average GPA for a student, and others to select student data using the GPA function. It shows how to use functions to select, update and return data in tables. The document demonstrates how to create and use SQL functions to encapsulate reusable code and perform queries on tables.

Uploaded by

HuyCường
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

FUNCTION

CREATE FUNTION function_name ( [@parameter_name parameter_data_type] )

RETURNS [return Data-‐type] /*Returns có ‘s’ */

AS Begin
…….

return ([scalar value])

End
CREATE FUNTION function_name ( [@parameter_name parameter_data_type] )

RETURNS table

AS
return [select command]

1. Write the function that returns the average score of @StudentID

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

Select ID, dbo.getGPA(ID) as ‘GPA’ from student

3. Use the above function to write the query for showing the list of students whose GPAs
are greater than 7.

Select s.* from student where dbo.getGPA(ID)>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)

5. Write a function that returns the student list of @ClassID.


CREATE function
GetStuList( @classID
varchar(10))
RETURNS table
AS
return select ID,name from
student
where classID=@classID

select P.name from


dbo.GetStuList('LH000001 ')
All the functions that SQL supplies

https://www.w3schools.com/sql/sql_ref_sqlserver.asp

You might also like