---------------------------------------------------------------------------------------------------------
/*使用游标处理数据*/
--使用游标,在表中逐行读取
select * from student
declare c_stu cursor for select * from student
open c_stu
fetch next from c_stu
while @@fetch_status = 0
fetch next from c_stu
close c_stu
deallcoate c_stu
-------------------------------------------------------------------------------------------------------
--fetch的使用
--1.first:移动到第一行,并作为当前行
--2.next:移动到下一行,并作为当前行
--3.prior:移动到上一行,并作为当前行
--4.last:移动到最后一行,并作为当前行
--5.absolute n:n>0时,从第一行开始,移动n行,作为当前行,n<0时,从最后一行倒数n行,作为当前行。
--6.relative n:n>0时,从当前行正数移动n行,作为当前行,n<0时,从当前行倒数n行,作为当前行。
--7.打开游标后第一次执行fetch next可以得到表中第一条数据,执行fetch prior得不到任何数据
--8.可用@@fetch_status返回执行fetch操作后游标的状态:
---- 0表示成功读取。
---- -1表示读取操作超出结果集。
---- -2表示行在表中不存在。
-------------------------------------------------------------------------------------------------------
--修改数据,将数据表中的第2行数据的姓名改成“张三”
select * from student
declare c_stu cursor for select * from student
open c_stu
fetch c_stu
fetch c_stu
update student set sname = '张三' where current of c_stu
close c_stu
deallocate c_stu
------------------------------------------------
--update student set sname = '张三' where current of c_stu
--如果去掉where current of c_stu,不进行指定,将更新表中所有字段
------------------------------------------------
--删除数据,将表中第2行数据删除
select * from student
declare c_stu cursor for select * from student
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)
-