(1)//功能是:
//输出一个表,条件满足:系别=sdept;课程名=name;
create function sc_inf(sdept varchar(10),name varchar(10))
return table
return
(
select stu.* sc.* course.*
from sc ,stu,course
where stu.sno=sc.sno and sc.cno=course.cno and sdept=@sdept and name=@name //作用是:将stu表 sc 表做一次链接
)
//那么我们如何调用它呢:
//用法如下:
select * from sc.info('计算机系',‘数据库’)
//我们就可以得到在计算机系选修数据库的学生信息了。
//拓展例子:
//求100以内的偶数和的函数:
create function sc_info()//无参数
return int //返回int 类型的数据
as
declare @sum int ,@i int
begin
set @sum=0
set @i=0
while i<=100
begin
set @i=@i+2
set @sum=@sum+@i
end
end
//扩展例子
//题目要求:(我也忘记这个叫什么东东了)
//比较两个数的大小,返回较大的那个数。
create proc max
declare @a int,@b int ,@c int
set @c=0;
begin
if a>b
set @c=@a;
else
set @c=@b;
end
print c;
//如何调用
//用法如下:
exec max 36 25
//结果返回36
//题目又要求了:
//如果我们要求输出一个表;(不用上面的函数)
create proc stu_inf @sno varchar(10)
as
select *
from stu
where sno=@sno
//执行:
exec stu_inf ('200215121')
//那么用函数实现呢?
create function stu_info (@ sno varchar(10))
return table
as
retrun
(
select *
from stu;
where sno=@sno;
)
//执行:
select* from dbo.stu_info('200215121')
//输出一个表,条件满足:系别=sdept;课程名=name;
create function sc_inf(sdept varchar(10),name varchar(10))
return table
return
(
select stu.* sc.* course.*
from sc ,stu,course
where stu.sno=sc.sno and sc.cno=course.cno and sdept=@sdept and name=@name //作用是:将stu表 sc 表做一次链接
)
//那么我们如何调用它呢:
//用法如下:
select * from sc.info('计算机系',‘数据库’)
//我们就可以得到在计算机系选修数据库的学生信息了。
//拓展例子:
//求100以内的偶数和的函数:
create function sc_info()//无参数
return int //返回int 类型的数据
as
declare @sum int ,@i int
begin
set @sum=0
set @i=0
while i<=100
begin
set @i=@i+2
set @sum=@sum+@i
end
end
//扩展例子
//题目要求:(我也忘记这个叫什么东东了)
//比较两个数的大小,返回较大的那个数。
create proc max
declare @a int,@b int ,@c int
set @c=0;
begin
if a>b
set @c=@a;
else
set @c=@b;
end
print c;
//如何调用
//用法如下:
exec max 36 25
//结果返回36
//题目又要求了:
//如果我们要求输出一个表;(不用上面的函数)
create proc stu_inf @sno varchar(10)
as
select *
from stu
where sno=@sno
//执行:
exec stu_inf ('200215121')
//那么用函数实现呢?
create function stu_info (@ sno varchar(10))
return table
as
retrun
(
select *
from stu;
where sno=@sno;
)
//执行:
select* from dbo.stu_info('200215121')