sql编程2 游标与存储过程

sql编程中的游标的使用:
提供的一种对查询的结果集进行逐行处理的一种方式
不用游标的处理解决方式:
逐行修改工资
update salar set 工资=‘新工资’ where 雇员号='0101' //通过查出雇员号而修改工资
过程:
1.定义一个游标(只想某个结果集的一个行指针) cursor 游标类型
例:
declare ll_cursor cursor for select * from students
2.打开游标
open ll_cursor
3.依次往下读取结果集中的每一行数据
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 [id]
,[son]
,[name]
,[sex]
,[age]
FROM [SchoolIDB].[dbo].[students]

declare @id int
declare @son nvarchar
declare @name nvarchar
declare @sex nvarchar
declare @age int
declare ll_cursor cursor for select * from students
open ll_cursor

fetch next from ll_cursor into @id,@son,@name,@sex,@age
print @id
print @son
print @name
print @sex
print @age
close ll_cursor //关闭游标
deallocate ll_cursor //释放资源

//注意:
Cursorfetch: INTO 列表中声明的变量数目必须与所选列的数目相同。必须与列名的数量相同 //id,name..等

游标的原理:通过在结果集中设立的一个指针来从上往下一行行遍历数据的一个过程

注意:
//在遍历完成后,需要关闭游标 并且释放掉资源

2.可以通过访问@@fetch_status 全局变量的值来判断当前是否读取到数据航
如果@@fetch_studet=0 说明读取成功
否则就是读取失败

存储过程:
预先编译好一段sql语句,用来实现特定得功能,类似c#中的方法
定义存储过程的语法
go
create procedure usp_过程名
as
sql语句
....
存储过程的命名规范:usp_功能
例:
定义一个课程名变量
@name nvarchar(25)
as
select @id=id from course where name=@name //通过name 名来找到id

调用存储过程:
execute 存储过程名
存储过程也分有参和无参
--调用、执行存储过程
exec proc_get_student; //在不同的地方调而不是在本程序调用

3、 修改存储过程

--修改存储过程
alter proc proc_get_student
as
select * from student;

4、 带参存储过程

--带参存储过程
if (object_id('proc_find_stu', 'P') is not null)
drop proc proc_find_stu
go
create proc proc_find_stu(@startId int, @endId int)
as
select * from student where id between @startId and @endId
go

exec proc_find_stu 2, 4;

5、 带通配符参数存储过程

--带通配符参数存储过程
if (object_id('proc_findStudentByName', 'P') is not null)
drop proc proc_findStudentByName
go
create proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%')
as
select * from student where name like @name and name like @nextName;
go

exec proc_findStudentByName;
exec proc_findStudentByName '%o%', 't%';

6、 带输出参数存储过程

if (object_id('proc_getStudentRecord', 'P') is not null)
drop proc proc_getStudentRecord
go
create proc proc_getStudentRecord(
@id int, --默认输入参数
@name varchar(20) out, --输出参数
@age varchar(20) output--输入输出参数
)
as
select @name = name, @age = age from student where id = @id and sex = @age;
go

--
declare @id int,
@name varchar(20),
@temp varchar(20);
set @id = 7;
set @temp = 1;
exec proc_getStudentRecord @id, @name out, @temp output;
select @name, @temp;
print @name + '#' + @temp;

7、 不缓存存储过程

--WITH RECOMPILE 不缓存
if (object_id('proc_temp', 'P') is not null)
drop proc proc_temp
go
create proc proc_temp
with recompile
as
select * from student;
go

exec proc_temp;

8、 加密存储过程

--加密WITH ENCRYPTION
if (object_id('proc_temp_encryption', 'P') is not null)
drop proc proc_temp_encryption
go
create proc proc_temp_encryption
with encryption
as
select * from student;
go

exec proc_temp_encryption;
exec sp_helptext 'proc_temp';
exec sp_helptext 'proc_temp_encryption';

9、 带游标参数存储过程

if (object_id('proc_cursor', 'P') is not null)
drop proc proc_cursor
go
create proc proc_cursor
@cur cursor varying output
as
set @cur = cursor forward_only static for
select id, name, age from student;
open @cur;
go
--调用
declare @exec_cur cursor;
declare @id int,
@name varchar(20),
@age int;
exec proc_cursor @cur = @exec_cur output;--调用存储过程
fetch next from @exec_cur into @id, @name, @age;
while (@@fetch_status = 0)
begin
fetch next from @exec_cur into @id, @name, @age;
print 'id: ' + convert(varchar, @id) + ', name: ' + @name + ', age: ' + convert(char, @age);
end
close @exec_cur;
deallocate @exec_cur;--删除游标

10、 分页存储过程

---存储过程、row_number完成分页
if (object_id('pro_page', 'P') is not null)
drop proc proc_cursor
go
create proc pro_page
@startIndex int,
@endIndex int
as
select count(*) from product
;
select * from (
select row_number() over(order by pid) as rowId, * from product
) temp
where temp.rowId between @startIndex and @endIndex
go
--drop proc pro_page
exec pro_page 1, 4
--
--分页存储过程
if (object_id('pro_page', 'P') is not null)
drop proc pro_stu
go
create procedure pro_stu(
@pageIndex int,
@pageSize int
)
as
declare @startRow int, @endRow int
set @startRow = (@pageIndex - 1) * @pageSize +1
set @endRow = @startRow + @pageSize -1
select * from (
select *, row_number() over (order by id asc) as number from student
) t
where t.number between @startRow and @endRow;

exec pro_stu 2, 2;

? Raiserror

Raiserror返回用户定义的错误信息,可以指定严重级别,设置系统变量记录所发生的错误。

语法如下:

Raiserror({msg_id | msg_str | @local_variable}
{, severity, state}
[,argument[,…n]]
[with option[,…n]]
)

# msg_id:在sysmessages系统表中指定的用户定义错误信息

# msg_str:用户定义的信息,信息最大长度在2047个字符。

# severity:用户定义与该消息关联的严重级别。当使用msg_id引发使用sp_addmessage创建的用户定义消息时,raiserror上指定严重性将覆盖sp_addmessage中定义的严重性。

任何用户可以指定0-18直接的严重级别。只有sysadmin固定服务器角色常用或具有alter trace权限的用户才能指定19-25直接的严重级别。19-25之间的安全级别需要使用with log选项。

# state:介于1至127直接的任何整数。State默认值是1。

raiserror('is error', 16, 1);
select * from sys.messages;
--使用sysmessages中定义的消息
raiserror(33003, 16, 1);
raiserror(33006, 16, 1);

sqL编程篇(三) 游标与存储过程的更多相关文章

  1. SQL编程篇 (二) 定义与流程控制

    分类: sql编程:标准的sql 编程 * 纯sql 在标准的编程中又分为 sqlserver-->T-sql oracle-->pl-sql(扩展) 变量:在使用变量之前先定义 声明变量 ...

  2. PL/SQL 编程(三 )程序包和包体,触发器,视图,索引

    一.程序包和包体 程序包(package):存储在数据库中的一组子程序.变量定义.在包中的子程序可以被其它程序包或子程序调用.但如果声明的是局部子程序,则只能在定义该局部子程序的块中调用该局部子程序. ...

  3. sql编程篇 (五) 事务

    计算机中的事务 编辑 概念 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务通常由高级数据库操纵语言或编程语言(如SQL,C++或Java)书写的用 ...

  4. Oracle学习2 视图 索引 sql编程 游标 存储过程 存储函数 触发器

    ---视图 ---视图的概念:视图就是提供一个查询的窗口,来操作数据库中的数据,不存储数据,数据在表中. ---一个由查询语句定义的虚拟表. ---查询语句创建表 create table emp a ...

  5. [推荐]ORACLE PL/SQL编程之四:把游标说透(不怕做不到,只怕想不到)

    原文:[推荐]ORACLE PL/SQL编程之四:把游标说透(不怕做不到,只怕想不到) [推荐]ORACLE PL/SQL编程之四: 把游标说透(不怕做不到,只怕想不到) 继上两篇:ORACLE PL ...

  6. 【强烈强烈推荐】《ORACLE PL/SQL编程详解》全原创(共八篇)--系列文章导航

    原文:[强烈强烈推荐]<ORACLE PL/SQL编程详解>全原创(共八篇)--系列文章导航 <ORACLE PL/SQL编程详解> 系列文章目录导航 ——通过知识共享树立个人 ...

  7. sql编程 && 存储过程

    sql  结构化查询语言      是一种编程语言   用于管理数据库的编程语言      元素:     数据      数据类型         变量的数据类型  就是字段的数据类型      变 ...

  8. PL/SQL编程—游标

    一.游标的相关概念: 定义: 游标它是一个服务器端的存储区,这个区域提供给用户使用,在这个区域里 存储的是用户通过一个查询语句得到的结果集,用户通过控制这个游标区域当中 的指针 来提取游标中的数据,然 ...

  9. PL/SQL编程基础(三):数据类型划分

    数据类型划分 在Oracle之中所提供的数据类型,一共分为四类: 标量类型(SCALAR,或称基本数据类型) 用于保存单个值,例如:字符串.数字.日期.布尔: 标量类型只是作为单一类型的数据存在,有的 ...

随机推荐

  1. myeclipse2015卸载、安装、破解全过程-----myeclipse2015

    myeclipse2015安装以及破解步骤: 下载地址:myeclipse2015-->https://pan.baidu.com/s/1i4RFCBb   密码:qxsu 破解文件地址--&g ...

  2. width:100%;与width:auto;的区别

    <div> <p>1111</p> </div> div{ width:980px; background-color: #ccc; height:30 ...

  3. Vim配置文件

    转载 原文网址:http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html 花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim ...

  4. 【转】理解inode

    From:http://www.ruanyifeng.com/blog/2011/12/inode.html  阮一峰大神真NB 作者: 阮一峰 日期: 2011年12月 4日 inode是一个重要概 ...

  5. Python爬虫Scrapy框架入门(3)

    往往需要爬取的网页是呈一个树状结构.比如,需要先爬取一个目录,然后再在目录中选择具体的爬取目标.而目录和具体目标之间,网页结构不同,使得我们不能使用相同的爬取策略. 从之前的经验来看,我们对scrap ...

  6. Android Studio一些常用快捷键及快捷键冲突解决

    1. 最近在自学Android,也是边看书边写一些Demo,由于知识点越来越多,脑子越来越记不清楚,所以打算写成读书笔记,供以后查看,也算是把自己学到所理解的东西写出来,献丑,如有不对的地方,希望大家 ...

  7. Delphi容器类之---Tlist,TStringlist,THashedStringlist的效率比较

    转载自:http://www.ylzx8.cn/windows/delphi/73200.html 本人在做一个测试,服务器是IOCP的,我假定最大链接数是50000个. 测试背景:如果每个链接之间的 ...

  8. oracle 11.2.0.4单实例文件系统安装与补丁

    [TOC] 一,预安装处理 1.版本准备 操作系统:RHEL 6.5 数据库版本:Oracle 11.2.0.4 相关包:p13390677_112040_Linux-x86-64_1of7.zip  ...

  9. 产品经理 - 移动支付+Pos收单分析

    产品经理 - 移动支付+Pos收单分析

  10. Mac下没有权限启动tomcat的解决办法

    问题描述 在Mac中通过./startup.sh执行启动脚本文件,启动tomcat时报如下错误: -bash: ./startup.sh: Permission denied 解决方法 错误信息说明了 ...