declare
@_substoreid varchar(50)='',
@_depart varchar(50)='',
@_win_name varchar(50)=''
--创建游标
declare @cursor cursor
--设定游标欲操作的数据集
set @cursor=cursor for
select substoreid,depart from cur_user group by substoreid,depart
--打开游标
open @cursor
--移动游标指向到第一条数据,提取第一条数据存放在变量中
fetch next from @cursor into @_substoreid,@_depart
--如果上一次操作成功则继续循环
while(@@fetch_status=0)begin
--操作提出的数据
declare @cursor_new cursor
set @cursor_new=cursor for
select win_name from win_name open @cursor_new
fetch next from @cursor_new into @_win_name
while(@@fetch_status=0)begin
-- begin tran
insert into user_win values('',@_win_name,'Y','',@_substoreid,@_depart)
-- if @@Error<>0
-- begin
-- print @_win_name+','+@_substoreid+','+@_depart
-- ROLLBACK TRANSACTION--事务回滚语句
-- end
-- else
-- begin
-- COMMIT TRANSACTION--事务提交语句
-- end
--
-- end fetch next from @cursor_new into @_win_name
end
close @cursor_new
deallocate @cursor_new --继续提下一行
fetch next from @cursor into @_substoreid,@_depart
end
--关闭游标
close @cursor
--删除游标
deallocate @cursor
declare
@_substoreid varchar(50)='',
@_date varchar(50)='2014-08-20',--开始时间
@_endDate varchar(50)='2014-08-22'--getdate()--结束时间(不包含当天) while(DATEDIFF(day,@_date,@_endDate)>0)begin
--创建游标
declare @cursor cursor
--设定游标欲操作的数据集
set @cursor=cursor for
select substoreid from hotelid
--打开游标
open @cursor
--移动游标指向到第一条数据,提取第一条数据存放在变量中
fetch next from @cursor into @_substoreid
--如果上一次操作成功则继续循环
while(@@fetch_status=0)begin
--操作提出的数据
if not exists(select id from receive_report where substoreid=@_substoreid and the_date=@_date and name='房型统计' and id='总房数')
begin
declare @curroomnums decimal(10,1)
select @curroomnums=isnull(all_rooms,-1) from receive_report where substoreid=@_substoreid and the_date=@_date and name='租类' and id='合计'
if @curroomnums is not null and @curroomnums>0
begin
declare @the_class varchar(20)='',
@total decimal(10,1) declare @cursor_new cursor
set @cursor_new=cursor for
select the_class,count(*) as total from home where substoreid=@_substoreid group by the_class open @cursor_new
fetch next from @cursor_new into @the_class,@total while(@@fetch_status=0)begin
insert into receive_report(id,substoreid,day_rooms,the_date,name,sort_flag)
values(@the_class,@_substoreid,@total,@_date,'房型统计','') fetch next from @cursor_new into @the_class,@total
end close @cursor_new
deallocate @cursor_new insert into receive_report(id,substoreid,day_rooms,the_date,name,sort_flag)
values('总房数',@_substoreid,@curroomnums,@_date,'房型统计','')
print @_substoreid+' '+@_date
end
end --继续提下一行
fetch next from @cursor into @_substoreid
end
--关闭游标
close @cursor
--删除游标
deallocate @cursor set @_date= convert(varchar(12), DATEADD(day,1,@_date),23)
end

SQL双重游标(双重循环)--笔记的更多相关文章

  1. sql server 存储过程---游标的循环

    sqlserver中的循环遍历(普通循环和游标循环) sql 经常用到循环,下面介绍一下普通循环和游标循环 1.首先需要一个测试表数据Student

  2. SQL Server 游标运用:鼠标轨迹字符串分割

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 游标模板(Cursor Template) 鼠标轨迹字符串分割SQL脚本实现(SQL Code ...

  3. 20180925 SQL Server游标使用

    之前写了一个存储过程 目标: 根据时间,获取指定范围的数据集A, 再从数据集A 中,取出每行数据中的两个时间,卡另外一个B表里面的某列时间范围的值集.得到均值进行配置值比对. 这里有个问题,就是取得B ...

  4. Oracle_PL/SQL(3) 游标

    引言:PLSQL数据类型标量数据类型:数字类.字符类.日期类.布尔类(boolean).复合数据类型:记录(%rowtype).表.数组引用类型:REF CURSORLOB类型:BLOB.CLOB 1 ...

  5. PL/SQL之游标的使用

    Oracle中的游标有两种: 显式游标 用CURSOR...IS 命令定义的游标,它可以对查询语句(SELECT)返回的多条记录进行处理. 隐式游标 是在执行插入(INSERT).删除(DELETE) ...

  6. Oracle学习2 视图 索引 sql编程 游标 存储过程 存储函数 触发器

    ---视图 ---视图的概念:视图就是提供一个查询的窗口,来操作数据库中的数据,不存储数据,数据在表中. ---一个由查询语句定义的虚拟表. ---查询语句创建表 create table emp a ...

  7. SQL Server 游标的应用

    ----------------SQL游标应用----------------- 今天由于业务需求,需要在存储过程中实现有一个表的主键去匹配在另一个表中作为外键所对应的数值 ,若在C#中则非常简单只需 ...

  8. SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)

    原文:SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database) 一.本文所涉及的内容(Contents) 本文所涉及的内容 ...

  9. SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database)

    原文:SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database) 一.本文所涉及的内容(Contents) 本文所涉及的内容(C ...

  10. PL/SQL使用游标CURSOR

    一.使用游标 对于DML语句和单行select into ,oracle自动分配隐形游标.处理select返回多行语句,可以使用显式游标. 使用显示游标处理多行数据,也可使用SELECT..BULK ...

随机推荐

  1. iOS - 落叶加载动画效果

    代码下载地址:https://github.com/nLoser/LeafLoadingView 效果: 说明:效果是在网上看到的,并且自己按照效果自己实现,树叶使用CAEmitterLayer做的, ...

  2. BZOJ3393:[USACO LPHONE] 激光通讯

    分层图+堆优化的dijkstra 将原图分为4层,分别是只向上,向下,向左,向右建立边,然后层与层之间的转移很好处理.稠密图,应该用堆优化的dijkstra. //OJ 1845 //by Cydia ...

  3. Yocto开发笔记之《根文件系统裁剪》(QQ交流群:519230208)

    开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 =============================== ...

  4. js007-函数表达式

    js007-函数表达式 本章内容 1.函数表达式的特征 2.使用函数实现递归 3.使用闭包定义私有量 定义函数的方式有两种:一:函数声明,二:函数表达式 函数声明: function function ...

  5. c# 操作datatable

    1.创建 datatable DataTable dt=new Datable();// 可以给表创建一个名字,tb 2.给表加个列名: dt.Columns.Add("id", ...

  6. JavaScript学习笔记——流程控制

    javascript流程控制流程:就是程序代码的执行顺序.流程控制:通过规定的语句让程序代码有条件的按照一定的方式执行. 一.顺序结构 按照书写顺序来执行,是程序中最基本的流程结构. 二.选择结构(分 ...

  7. array_filter移除空数组

    $arr = array( "0"=> "564645", "1"=>"", "2"=& ...

  8. Nancy总结(二)记一次Nancy 框架中遇到的坑

    记一次Nancy 框架中遇到的坑 前几天,公司一个项目运行很久的Nancy框架的网站,遇到了一个很诡异的问题.Session 对象跳转到另外一个页面的时候,session对象被清空了,导致用户登录不上 ...

  9. nginx配置图片防盗链

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${ expires 30d; access_log off; valid_referers none blocked ...

  10. msbuild FileSysExcludeFiles

    <?xml version="1.0" encoding="utf-8"?> <!-- This file is used by the pu ...