Sqlserver循环嵌套
生活随笔
收集整理的這篇文章主要介紹了
Sqlserver循环嵌套
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.游標的狀態,游標的開啟游標的選擇都是需要注意的。
USE [ccnu] GO/****** Object: StoredProcedure [dbo].[P_ADD_DATA_XSBLHYCQK] Script Date: 2015/2/13 10:32:46 ******/ SET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER ON GO-- ============================================= -- Author: <Author,,Zen> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= CREATE PROCEDURE [dbo].[P_ADD_DATA_XSBLHYCQK] AS declare @fdy varchar(200),@xs_id varchar(200),@bzr varchar(200),@sex varchar(200); declare @kind varchar(200),@ssbh varchar(200),@xyan varchar(200),@class varchar(200); declare @bbq varchar(200); declare @swsc numeric; -- 上網時長 declare @ZHHSSSJ time; -- 最后回宿舍時間 declare @day int ;-- 天數DECLARE @RandomNumber float DECLARE @RandomInteger int; DECLARE @MaxValue int; DECLARE @MinValue int;BEGINset @day = 0;--打開游標 while @day<14beginselect @bbq=CONVERT(nvarchar(8),cast('2015-02-01' as datetime) +@day,112)declare cur_JQLXXX CURSOR FOR SELECT [FDY],[BZR],[CLASS],[SSBH],[SEX],[ID],[KIND],[DEPT]FROM [ccnu].[dbo].[WG_JQLXXX0212];open cur_JQLXXX --開始循環游標變量 FETCH NEXT FROM cur_JQLXXX INTO @fdy,@bzr,@class,@ssbh,@sex,@xs_id,@kind,@classWHILE @@FETCH_STATUS = 0 begin SET @MaxValue = 5*60*60SET @MinValue = 1*60*60SELECT @RandomNumber = RAND()SELECT @swsc = ((@MaxValue + 1) - @MinValue) * @RandomNumber + @MinValueselect @ZHHSSSJ=dateadd(second,ceiling(rand() * 25200)+61200,cast('2015-02-01' as datetime)+@day) --執行sql操作insert into [ccnu].[dbo].WG_XSBLHYCQK0212([BBQ],[XS_ID],[XB],[XS_TYPE],[SUSE_ID],[XYUAN],[ZYE],[BJI],[BZR],[FDY],[SFWG],[SFYC],[SWZSC],[FZCSDSWSC],[YCKSRQ],[YCJSRQ],[ZHHSSSJ])select @bbq,@xs_id,@sex,@kind,@ssbh,@xyan,NULL,@class,@bzr,@fdy,0,0,@swsc,0,null,null,@ZHHSSSJFETCH NEXT FROM cur_JQLXXX INTO @fdy,@bzr,@class,@ssbh,@sex,@xs_id,@kind,@class --取下一條數據endCLOSE cur_JQLXXX --關閉游標deallocate cur_JQLXXX --釋放游標,寫循環嵌套的時候一定要注意內層游標一定要在外層游標的包含內聲明、關閉或釋放。select @day=@day+1endENDGO?
轉載于:https://www.cnblogs.com/Alex-Zeng/p/4289818.html
總結
以上是生活随笔為你收集整理的Sqlserver循环嵌套的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Replication的犄角旮旯(五)-
- 下一篇: sama5d3 xplained 系统加