• 【Vegas原创】SQL Server游标的经典使用


    昨天查关于游标的东西,发现我博客竟然没有~

    整理了一下,记录于此,以便以后查询使用。

    USE [PACSMonitor]
    GO
    /****** Object:  StoredProcedure [dbo].[sp_GetPSExamCountByDay]    Script Date: 04/22/2010 14:35:14 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[sp_GetPSExamCountByDay]
    AS
    DECLARE @date nvarchar(50)
     
    --先干其他的事情
    insert into dbo.rpt_psExamCountByDay(rptDate)
    select distinct substring((ex_cdt),1,8) from v_psExam 
    where substring((ex_cdt),1,8) COLLATE SQL_Latin1_General_CP1_CI_AS not in(select distinct rptdate from rpt_psExamCountByDay)
     
    --声明游标变量,取得数据--
    DECLARE contact_cursor CURSOR FOR
    SELECT rptDate FROM rpt_psExamCountByDay  
     
     
    --打开游标
    OPEN contact_cursor 
     
     
    FETCH NEXT FROM contact_cursor into @date  --开始抓第一条数据
    WHILE(@@fetch_status=0) 
        BEGIN     
            update rpt_psExamCountByDay
            set rptCount=(
                select COUNT(1) from v_psExam 
                where substring((ex_cdt),1,8) collate SQL_Latin1_General_CP1_CI_AS=@date)
                where rptDate=@date
                
            FETCH NEXT FROM contact_cursor into @date     --跳到下一条数据    
        END
     
     
    --关闭游标
    CLOSE contact_cursor
    --删除游标
    DEALLOCATE contact_cursor
     
     

    参考文档:http://msdn.microsoft.com/zh-cn/library/ms180152.aspx

    喜欢请赞赏一下啦^_^
  • 相关阅读:
    prometheus监控示例
    es索引管理工具-curator
    GlusterFS分布式文件系统的使用
    Django的admin相关
    Django的form表单
    Django-models & QuerySet API
    python-css基础知识
    PXC 避免加入集群时发生SST
    ERROR 1682 (HY000)
    pxc群集搭建
  • 原文地址:https://www.cnblogs.com/amadeuslee/p/3744167.html
Copyright © 2020-2023  润新知