SQLServer令总结 下载本文

内容发布更新时间 : 2024/5/3 10:55:47星期一 下面是文章的全部内容请认真阅读。

open c_stu fetch c_stu fetch c_stu

delete from student where current of c_stu close c_stu deallocate c_stu 函数

/*-------------------------------------- 自定义函数的种类: √ 1.标量函数 √ 2.内嵌表函数 3.多语句表值函数 --------------------- 标量函数基本语法 create function 函数名 returns 返回的参数类型 as begin 函数体

return 函数返回的标量值 end

--------------------------

内嵌表函数语法

create function 函数名 returns table as return (select查询语句)

-------------------------------------*/ /*---------------------------------------- 注意:函数是可以带参数的,但是参数不可以是: 1.时间戳(timestamp) 2.游标(cursor) 3.表(table)

----------------------------------------*/

/*------------------------------------------------------------------------ 标量函数

create function chinacode(@str varchar(255)) returns char(2) as begin

declare @i int,@j int set @i = len(@str) set @j = 1 while (@j<=@i) begin

if(unicode(substring(@str,@j,1))<256) return '否' set @j = @j + 1 end return '是' end

select dbo.chinacode('我是中国人') select dbo.chinacode('我,是中国人') select dbo.chinacode('ilovechina')

------------------------------------------------------- substring(expression , start , length ) 字符串截取函数 expression :字符串

start :是一个整数,表示截取的位置 length :是一个整数,表示一次截取的长度

-------------------------------------------------------

-------------------------------------------------------------------------*/

/*------------------------------------------------------------------------

内嵌表函数

------------------------------------------------------------------------- 1.内嵌表

create function age(@maxage int,@minage int) returns table as

return(select * from tbl_stu where stuage < @maxage and stuage > @minage)

select * from age(13,11)

------------------------------------------------------------------------ 2.内嵌视图

create function grade(@ss float) returns table as

return(select * from stu_cj_view where sumscore > @ss)

select * from grade(200)

-------------------------------------------------------------------------*/

/*--------------------------------------- 修改函数

把create function写成alter function 删除函数

drop function 函数名

----------------------------------------*/