设为首页 收藏本站
查看: 1057|回复: 0

[经验分享] sql server function

[复制链接]

尚未签到

发表于 2016-10-29 10:42:00 | 显示全部楼层 |阅读模式
一、.返回数值的用户自定义函数(标量函数)
create function 函数名称
(
?参数1 类型定义1,
?参数2 类型定义2,
?...
)
returns 返回值类型
with encryption
as
begin
?函数体代码?
?return 返回值
end

--/////
create function fun001()
returns varchar(20)
as
begin
?return 'Welcome to China'
end

--select getdate()
select dbo.fun001()

--jisuan(n)=1+2+3+...+n

create function jisuan(@n int)
returns int
as
begin
?declare @s int,@i int
?set @s=0
?set @i=1
?while (@i<=@n)
?begin
set @s=@s+@i
set @i=@i+1
?end
?return @s
end


select dbo.jisuan(100)

select dbo.jisuan(10)

--统计城市获取人数
create function fun002(@city varchar(20))
returns int
as
begin
?declare @count int
?select @count=count(*) from student
where city=@city
?return @count
end


select dbo.fun002('北京')


select distinct city from student
select distinct city,dbo.fun002(city) as num from student

print dbo.fun002('北京')

--(1)通过函数完成:返回两个整数的最大值
create function fun003(@a int,@b int)
returns int
as
begin
declare @s int

?if (@a>@b)
?set @s=@a
?else
?set @s=@b
return @s
end
--函数中最后一条语句必须是返回语句。
select dbo.fun003(34,5656)
select dbo.fun003(34,3)
--(2)通过函数完成:返回三个整数的最大值
/*
a b c
--fun003(fun003(a,b),c)

a>b
?a c
else
?b c
*/
create function fun004(@a int,@b int,@c int)
returns int
as
begin?
?return dbo.fun003(dbo.fun003(@a,@b),@c)
end

select dbo.fun004(23,5,2323)
select dbo.fun004(23,5232,23)
select dbo.fun004(2334,-5232,23)

create function fun005(@a int,@b int,@c int)
returns int
as
begin
?declare @s int
?if (@a > @b)
?begin
--返回a c 最大值
if (@a > @c)
set @s=@a
else
set @s=@c
?end
?else
?begin
--返回b c最大值
if (@b > @c)
set @s=@b
else
set @s=@c
?end
return @s
end


select dbo.fun005(23,5,2323)
select dbo.fun005(23,5232,23)
select dbo.fun005(2334,-5232,23)

--通过姓名统计总分

create function fun006(@name varchar(20))
returns numeric(10,2)
with encryption
as
begin
declare @s numeric(10,2)
if exists(select * from student where name=@name)
begin
?if exists(select * from relation where stu_no in(select stu_no from student where name=@name))
?begin
select @s=sum(mark) from relation where stu_no in (select stu_no from student where name=@name)
?end
?else
?begin
set @s=-2
?end
end
else
begin
?set @s=-1
end
return @s?
end

?
select dbo.fun006('abc')
select dbo.fun006('张三')
select dbo.fun006('陈刚')
select name,dbo.fun006(name) as summark from student

select name,dbo.fun006(name) as summark from student
where dbo.fun006(name) >= 0

--根据课程名称统计平均分
create function fun111(@c_name varchar(20))
returns numeric(5,2)
with encryption
as
begin
declare @s numeric(5,2)
?if exists(select * from course where c_name=@c_name)
?begin
if exists()
begin
end
else
begin
nd
?end
?else
?begin
set @s=-1
?end
return @s
end

?

二、内联(单语句)的返回表的用户自定义函数
create function 函数名称
(
?参数1 类型定义1,
?参数2 类型定义2,
?...
)
returns table
with encryption
as
return select语句


--按照城市获取学生信息

create function fun007(@city varchar(20))
returns table
with encryption
as
return
select * from student where city=@city

select * from dbo.fun007('北京')
select * from fun007('北京')

?
--按照姓名统计此人选修的课程基本信息
create function fun008(@name varchar(20))
returns table
with encryption
as
return
select * from course where c_no in(
select c_no from relation where stu_no in(
select stu_no from student where name=@name))


select * from fun008('陈刚')
select * from fun008('张三')
select * from fun008('abc')


--按照课程名称统计学修这门课程的学生信息
create function fun113(@c_name varchar(20))
returns table
with encryption
as
return
select * from student where stu_no in(
select stu_no from relation where c_no in(select c_no from course where c_name=@c_name))

?
select * from fun113('网站设计')

?
三、.多语句的返回表的用户自定义函数
create function 函数名称
(
?参数1 类型定义1,
?参数2 类型定义2,
?...
)
returns @return_variable table (定义)
[with encryption]
[as]
begin
function_body
return
end

--按照城市统计给城市的学生的姓名 性别 和城市

create function fun009(@city varchar(20))
returns @tab table(
name varchar(20),
sex char(2),
city varchar(20)
)
with encryption
as
begin
?insert into @tab(name,sex,city) select name,sex,city from student where city=@city
?return
end


select * from fun009('北京')

?

--统计全体学生的姓名和总分

create function fun010()
returns @tab table(
name varchar(20),
summark numeric(10,2)
)
with encryption
as
begin
declare @name varchar(20)
declare @summark numeric(10,2)
declare cur cursor for select name from student where stu_no in(select stu_no from relation)
open cur
fetch cur into @name
while (@@fetch_status=0)
begin
?select @summark=sum(mark) from relation where stu_no in (
?select stu_no from student where name=@name?)
?insert into @tab(name,summark) values(@name,@summark)
?fetch cur into @name
end
close cur
deallocate cur
return
end


select * from fun010()

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-292822-1-1.html 上篇帖子: SQL Server ——表 下篇帖子: SQL SERVER  2008
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表