--- SQLSERVER 循环
declare @num int
declare @datediff int
select @datediff=datediff(day,'2007-12-12','2008-9-10)
set @num=0
while @num<@datediff
begin
insert into cms_count values('192.168.1.174','1','狗狗','http://gougou.com/','1280*780','IE','Vasta','www.163.com',dateadd(day,@num,'2007-12-12') )
set @num=@num+1
end
--- 游标
DECLARE My_Cursor CURSOR --定义游标
FOR (SELECT * FROM dbo.MemberAccount) --查出需要的集合放到游标中
OPEN My_Cursor; --打开游标
FETCH NEXT FROM My_Cursor ; --读取第一行数据
WHILE @@FETCH_STATUS = 0
BEGIN
--UPDATE dbo.MemberAccount SET UserName = UserName + 'A' WHERE CURRENT OF My_Cursor; --更新
--DELETE FROM dbo.MemberAccount WHERE CURRENT OF My_Cursor; --删除
FETCH NEXT FROM My_Cursor; --读取下一行数据
END
CLOSE My_Cursor; --关闭游标
DEALLOCATE My_Cursor; --释放游标
GO
-------------------------------------------------------
Sql中的游标指针的位置判断:
0 FETCH 语句成功
-1 FETCH 语句失败或此行不在结果集中
-2 被提取的行不存在
@@fetch_status 值的改变是通过 fetch next from 实现的
“FETCH NEXT FROM Cursor”
0 的时候,游标查询的结果有,游标就会将查询到的数据直接存入游标捕捉数据的变量中
--------------------------------