zoukankan      html  css  js  c++  java
  • 【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

    喜欢请赞赏一下啦^_^
  • 相关阅读:
    区间DP+next求循环节 uva 6876
    连分数(分数类模板) uva6875
    二分+最短路 uvalive 3270 Simplified GSM Network(推荐)
    叉积判断 POJ1696
    树形DP+树状数组 HDU 5877 Weak Pair
    团 大连网赛 1007 Friends and Enemies
    微信支付少一分钱
    数据类型的转换
    什么是机器学习
    config.m4
  • 原文地址:https://www.cnblogs.com/amadeuslee/p/3744167.html
Copyright © 2011-2022 走看看