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. linux下svn 搭建

    linux 下SVN搭建加自动提交更新到指定目录一.安装#yum install subversion       yum安装SVN#mkdir -p /opt/svn/repos       创建s ...

  2. 泛在传感器网络(Ubiquitous Sensor Network; USN)

    http://wiki.mbalib.com/wiki/%E6%B3%9B%E5%9C%A8%E4%BC%A0%E6%84%9F%E5%99%A8%E7%BD%91%E7%BB%9C 什么是泛在传感器 ...

  3. BZOJ4445: [Scoi2015]小凸想跑步

    裸半平面交. 记得把P0P1表示的半平面加进去,否则点可能在多边形外. #include<bits/stdc++.h> #define N 100009 using namespace s ...

  4. RAL 标准颜色表(RAL Color Chart)

    根据经典RAL系统,本网页显示RAL标准颜色的概览.RAL用于信息,为图画和涂层定义标准颜色.现在,它是最流行的中欧颜色标准.这些颜色广泛用于建筑学,建筑物,工业和道路安全. 图表中的RAL颜色尽可能 ...

  5. python线程锁

    import time,threading balance = 0 lock = threading.Lock() def change_it(n): global balance balance = ...

  6. javascript的propertyIsEnumerable()方法使用介绍

    hasOwnProperty() 方法用来判断某个对象是否含有指定的自身属性. propertyIsEnumerable()是用来检测属性是否属于某个对象的,如果检测到了,返回true,否则返回fal ...

  7. swiper超出部分出现滚动条

    html代码 <div class="wrap swiper-container"> <div class="swiper-wrapper"& ...

  8. Redis总结(三)Redis 的主从复制

    接着上一篇,前面两篇我总结了<Redis总结(一)Redis安装>和<Redis总结(二)C#中如何使用redis> 所以这一篇,会讲讲Redis 的主从复制以及C#中如何调用 ...

  9. OC-Q&A

    How to declare a string in Objective-C ? A C string is just like in C. char myCString[] = "test ...

  10. js实现-下拉列表左右选择

    下拉列表左右选择          * 下拉选择框               <select>                     <option>111</opt ...