按月存档: 02月 2008

Sql 游标

Sql 游标,是一个很好用的东东,下面给出它的一个示例:
declare my_cursor1 cursor for   –为所获得的数据集指定游标
select nContentId,dtEditTime from content where datepart(month,dtEditTime)=’9′ and datepart(day,dtEditTime)=’26′
open my_cursor1  –打开游标
declare @date sysname
declare @nID sysname
declare @tempDate datetime
fetch next from my_cursor1 into @nID,@date   –开始抓第一条数据
while(@@fetch_status = 0)   –如果数据集里一直有数据
begin
    set @tempDate =  dateadd(day,87,@date)    –开始做想做的事(什么更新呀,删除呀)
    –print @tempDate
    update Content set dtEditTime=@tempDate where nContentId = @nID
    fetch next from my_cursor1 into @nID,@date   –跳到下一条数据
end
close my_cursor1   –关闭游标
deallocate my_cursor1  –删除游标
用于把9月26日的日期改为:12月22日。
游标好比Sql中的指针。