君仙的游标和局部函数
简简单单的游标和局部函数仅此而已
create database D13
go
use D13
create table UserInfo
(
ID int primary key identity,
Account varchar(20),
Password varchar(20),
Status int
)
insert into UserInfo values
('Test1','test1',0),
('Test2','test2',0),
('Test3','test3',0),
('Test4','test4',0),
('Test5','test5',0),
('Test6','test6',0),
('Test7','test7',0),
('Test8','test8',0),
('Test9','test9',0),
('Test10','test10',0)
create table UserStatus
(
Id int primary key identity,
uid int,
isdelete int
)
insert into UserStatus values
(1,1),
(2,1),
(3,1),
(4,1),
(5,1),
(6,1),
(7,1),
(8,1),
(9,1),
(10,1)
--定义游标
declare my_cursor cursor for
select ID from UserInfo
--打开游标
open my_cursor
--定义变量
declare @TID int
--循环读取并赋值
fetch next from my_cursor into @TID
while @@FETCH_STATUS=0
begin
update UserStatus set isdelete=0 where uid=@TID
--获取下一数据并赋值
fetch next from my_cursor into @TID
end
--关闭游标
close my_cursor
--删除游标
deallocate my_cursor
select * from UserStatus
--1.游标的优劣势是什么?
--答:优点:可以对select返回的行集合中的每一行执行不同操作
--缺点:处理大量数据时,效率低,占内存大
--2.@@CURSOR_ROWS什么?
--答:是一个全局变量,用来记录当前游标的数量,即从表中加载到游标中的行数
--3.什么是索引?
--答:索引是一种结构,可以快速访问数据库表中特定信息的结构