游标的用法,记录一下,老是忘记。。。。真是年纪大了
2011-02-21 15:56 Rainbow 阅读(305) 评论(0) 编辑 收藏 举报
declare @autoid int
declare @username varchar(50)
declare @userpwd varchar(32)
declare cursor1 cursor for
select autoid,username,userpwd from user_core_temp --这个有几列 你在游标赋值时就能给几个变量赋值
open cursor1
fetch next from cursor1 into @autoid,@username,@userpwd --对应刚刚出表中select出来的列
while @@FETCH_STATUS=0 --游标打开成功
begin
if exists(select 1 from User_Core where UserName=@username) --如果用户存在,就开始更新原来的密码
begin
update User_Core set UserPwd=@userpwd where UserName=@username
select @username
end
else --用户不存在就开始插入
begin
insert into User_Core
(
UserName,
UserPwd,
Email,
Mobile,
SencondPwd,
Recommender,
RealName,
CardNum,
Brithday,
Phone,
RegIP,
ProtectQuestion,
ProtectAnswer,
UserQQ,
UserMsn,
CreateTime,
IsActive,
ActiveCode,
IsAdult
)
select
UserName,
UserPwd,
Email,
Mobile,
SencondPwd,
Recommender,
RealName,
CardNum,
Brithday,
Phone,
RegIP,
ProtectQuestion,
ProtectAnswer,
UserQQ,
UserMsn,
CreateTime,
IsActive,
ActiveCode,
IsAdult
from User_Core_temp where username=@username
select '插入成功'
end
fetch next from cursor1 into @autoid,@username,@userpwd --记得要把下移 下一条记录语句放到一起,要不就是个死循环,一直在取第一条记录
end
close cursor1
deallocate cursor1
declare @username varchar(50)
declare @userpwd varchar(32)
declare cursor1 cursor for
select autoid,username,userpwd from user_core_temp --这个有几列 你在游标赋值时就能给几个变量赋值
open cursor1
fetch next from cursor1 into @autoid,@username,@userpwd --对应刚刚出表中select出来的列
while @@FETCH_STATUS=0 --游标打开成功
begin
if exists(select 1 from User_Core where UserName=@username) --如果用户存在,就开始更新原来的密码
begin
update User_Core set UserPwd=@userpwd where UserName=@username
select @username
end
else --用户不存在就开始插入
begin
insert into User_Core
(
UserName,
UserPwd,
Email,
Mobile,
SencondPwd,
Recommender,
RealName,
CardNum,
Brithday,
Phone,
RegIP,
ProtectQuestion,
ProtectAnswer,
UserQQ,
UserMsn,
CreateTime,
IsActive,
ActiveCode,
IsAdult
)
select
UserName,
UserPwd,
Email,
Mobile,
SencondPwd,
Recommender,
RealName,
CardNum,
Brithday,
Phone,
RegIP,
ProtectQuestion,
ProtectAnswer,
UserQQ,
UserMsn,
CreateTime,
IsActive,
ActiveCode,
IsAdult
from User_Core_temp where username=@username
select '插入成功'
end
fetch next from cursor1 into @autoid,@username,@userpwd --记得要把下移 下一条记录语句放到一起,要不就是个死循环,一直在取第一条记录
end
close cursor1
deallocate cursor1