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

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

 

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​