--视图是一种基于表的数据库查询,视图属于一种虚拟表,使用户执行数据查询时所显示的对象,本身不含有数据,其数据是动态生成的
use myg
go
if exists(select table_name from information_schema.views where table_name='shitu')
drop view shitu
go
create view shitu(a,b,c,d)
as
select bianhao as a,xingming as b,chushengnianyue as c,gongzi as d from biaoyi inner join biaoer on biaoyi.bianhao=biaoer.baihao
go
--利用视图进行表的增、删、改、查
use myg
go
create view lianxi(编号,姓名,c成绩, vb成绩)
as
select bianhao,xingming,c,vb from chengjibiao
go
delete from lianxi where c<59
go
insert into lianxi values(1,'myg','45','85')
go
update lianxi
set c成绩='62'
where c成绩 ='45'
go
select * from lianxi
--视图的修改
alter view mingcheng
--游标的使用方法
--允许用户访问以行为单位的数据,而不仅仅是对整个行集进行操作,应用程序并不能每次对结果集进行有效处理,游标提供了机制并扩展结果集
--把游标比作指针,把它获得的数据放入变量中。
--sql server支持以下两种请求的游标方法
--1.transact-sql
--2.数据库应用程序接口游标函数
--注意:应用程序不能使用两种方法请求游标,只有所有api游标属性都定为默认值时,才能用declare cursor 语句
--api游标的分类:静态、动态、前向、键集驱动
--创建游标的方法:
1。声明游标:确定游标的属性,制定游标的查询结果集
2。打开游标:编译sql server定义游标的select语句,并行成结果集
3。获取数据:通过游标以行的单位从结果集中获取数据
4。关闭游标:停止处理查询
5。释放游标:释放分配给游标的所有存储资源
--游标的使用
获取数据:fetch
next 紧跟结果集中当前行之后的结果行,若第一次提取,则返回第一行
prior 返回紧跟当前行前面的结果行, 若第一次提取,则没有返回行必将游标至于第一行
first 返回结果集中的第一行
last 返回最后一行并作为当前行
absolute{n|@nvar} 如果n>0,返回从游标头开始的第n行并将返回的行变成新的当前行
如果n<0,返回从游标尾开始的第n行并将返回的行变成新的当前行
如果n=0,没有行返回
relative{n|@nvar}如果n>0,返回从当前行之后的第n行并将返回的行变成新的当前行
如果n<0,返回从当前行之前的第n行并将返回的行变成新的当前行
如果n=0,没有行返回
global 游标是全局的
local 游标是局部的
--实例:
use myg
go
declare @标头 varchar(80),@价格 money ,@信息 varchar(150)
print '---------------报价单-----------------------'
print ''
print''
print ' '
print ' 产品名称 价格 状态 '
--声明游标
declare 游标 cursor for select 编号,价格 from 图书表 order by 价格
--打开
open 游标
--得到数据
fetch next from 游标 into @标头,@价格
while @@fetch_status=0
begin
if @标头>10
begin
select @信息=convert(char(50),@标头)+'$'+convert(char(8),@价格)+' 大于10'
print @信息
end
else
begin
select @信息=convert(char(50),@标头)+'$'+convert(char(8),@价格)+' 小于10'
print @信息
end
fetch next from 游标 into @标头,@价格
end
--关闭
close 游标
--释放
deallocate 游标
go
--2。通过游标提取特定行数据
use pubs
go
--变量声明
declare @a varchar(80),@b varchar(80)
print '----------------姓名 -------------------------'
print ' '
--查找
select au_lname,au_fname from biao order by au_lname,au_fname
--声明scroll cursor为可滚动的游标
declare youbiao scroll cursor for
select au_lname,au_fname from biao order by au_lname,au_fname
--打开
open youbiao
print '-----------------------结果---------------------'
print ' '
--获取最后一行
fetch last from youbiao into @a,@b
print convert(char(30),@a+''+@b)+' :最后一行'
--获取当前行的前一行
fetch prior from youbiao into @a,@b
print convert(char(30),@a+''+@b)+' :当前行的第一行'
--获取游标的第二行
fetch absolute 2 from youbiao into @a,@b
print convert(char(30),@a+''+@b)+' :第二行'
--获取 当前行的后面第三行数据
fetch relative 3 from youbiao into @a,@b
print convert(char(30),@a+''+@b)+' :当前行后的第三行'
--获取 当前行的 前 面第二行数据
fetch relative -2 from youbiao into @a,@b
print convert(char(30),@a+''+@b)+' :当前行前的第二行'
--关闭
close youbiao
--释放
deallocate youbiao
go
--3.通过游标更新数据
use myg
go
declare @标头 varchar(80),@价格 money ,@信息 varchar(150)
print '---------------报价单-----------------------'
print ''
print''
print ' '
print ' 产品名称 价格 状态 '
declare 游标 cursor for select 编号,价格 from 图书表 order by 价格
for update of 价格
--打开
open 游标
--得到数据
fetch next from 游标 into @标头,@价格
while @@fetch_status=0
begin
if @标头>10
begin
update 图书表
set 价格=@价格+200
where current of 游标
end
else
begin
update 图书表
set 价格=@价格+100
where current of 游标 --当前游标
end
fetch next from 游标 into @标头,@价格
end
--关闭
close 游标
--释放
deallocate 游标
go