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

2年前 (2022) 程序员胖胖胖虎阿
218 0 0

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

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

 

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

版权声明:程序员胖胖胖虎阿 发表于 2022年11月7日 下午9:56。
转载请注明:【Vegas原创】SQL Server游标的经典使用 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...