SQLServer令总结 下载本文

内容发布更新时间 : 2024/5/17 16:50:37星期一 下面是文章的全部内容请认真阅读。

-------------------------------------------------------------- 首次执行下列语句都会自动启动一个隐性事务:

Alter table,create,delete,drop,fetch(读取),grant(授予),insert,open,revoke(撤销),select,truncate table,update 事务中不可以使用的语句: 1. 数据库创建:create database 2. 数据库修改:alter database 3. 数据库删除:drop database

4. 数据库备份:dump database,backup database 5. 数据库还原:load database,restore database 6. 事务日志备份:dump transaction,backup log 7. 事务日志还原:load transcation,restore log 8. 配置:reconfigure 9. 磁盘初始化:disk init 10. 11.

更新统计数据:updata statistics 显示或设置数据库选项:sp_dboptin

游标

游标:允许用户能够从select语句查询的结果集中,逐条访问纪录,可以根据用户需要逐行的显示、修改、删除这些记录的数据访问处理机制。(游标就是数据表上的指针)

使用游标的步骤:

(1) 声明游标

declare 游标名 cursor for select 语句 (2) 打开游标 open 游标名

1) 当游标被打开时,指针会自动指在第一行;

2) 打开游标后,如果@@error=0表示游标打开操作成功; 3) 打开游标后,可以用@@cursor_rows返回纪录行数;

(3) 处理数据:

a) 移动到当前行并读取数据 fetch游标名 b) 删除当前行数据

delete from 表名/视图名 where current of游标名 c) 修改当前行数据

update from表名/视图名 set 列名=表达式 where current of 游标名 (4) 关闭游标 close 游标名 (5) 释放游标 deallocate 游标名 游标类型:

(1)static:静态,当用户A察看表中记录的同时,如果有用户B修改表中记录,

A用户将不会看到B修改的纪录,只看A使用open打开表时所看到的纪录。 (2)Dynamic:动态,当用户A察看表中记录的同时,如果有用户B修改表中记录,

A用户将看到B修改的纪录,也就是表中记录会不断的刷新,但是会耗费较大的系统资源。

(3)Forward Only:仅向前,游标仅能向前滚动。 (4)Scroll:滚动,游标可以前后左右滚动。 select * from student

/*打开游标,读取数据*/

declare c_stu cursor keyset for select * from student open c_stu if @@error = 0

print '学生总数' + convert(char(5),@@cursor_rows) else

print '读取学生数出错!' close c_stu deallocate c_stu

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

--keyset:键集游标,当游标打开时,在tempdb数据库中自动创建keyset表,用来记录游标读取的数据(仅纪录表示字段)

--------------------------------------------------------------------------------------------------------- /*使用游标处理数据*/

--使用游标,在表中逐行读取 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