隐藏

sql游标实例-定义游标-打开游标-关闭游标-释放游标

发布:2022/12/7 17:27:35作者:管理员 来源:本站 浏览次数:584

定义游标
--***********************************************************
DECLARE @title varchar(50),@urlkey varchar(50),@url varchar(150)
DECLARE cursor_name CURSOR FOR --定义游标
    SELECT top 1 title,urlkey,url FROM StatisticsName -- and IUIdInt in(40579)
OPEN cursor_name --打开游标
FETCH NEXT FROM cursor_name INTO  @title,@urlkey,@url  --抓取下一行游标数据
WHILE @@FETCH_STATUS = 0
    BEGIN
        --print(@PhoneStr)
        if len(@urlkey)>2
            begin
                print '正在统计:'+@title+'【'+CAST(DATEADD(DAY, -1, GETDATE()) AS varchar)+'】访问IP数据'
                DECLARE @ipcount int
                DECLARE @time datetime
                set @time=CAST(DATEADD(DAY, -1, GETDATE()) AS date)
                select @ipcount=count(1) from ( select vistip from VisitLog where DATEDIFF(d,VistTime,getdate())=1 and Url like '%'+@urlkey+'%' group by vistip) a
                print @ipcount
                insert into IPStatistics(title,urlkey,url,ipcount,[time])values(@title,@urlkey,@url,@ipcount,@time)
            end
        
        FETCH NEXT FROM cursor_name INTO @title,@urlkey,@url
    END
CLOSE cursor_name --关闭游标
DEALLOCATE cursor_name --释放游标

--*************************************************************