SQL双重游标(双重循环)--笔记
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双重游标(双重循环)--笔记的更多相关文章
- sql server 存储过程---游标的循环
sqlserver中的循环遍历(普通循环和游标循环) sql 经常用到循环,下面介绍一下普通循环和游标循环 1.首先需要一个测试表数据Student
- SQL Server 游标运用:鼠标轨迹字符串分割
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 游标模板(Cursor Template) 鼠标轨迹字符串分割SQL脚本实现(SQL Code ...
- 20180925 SQL Server游标使用
之前写了一个存储过程 目标: 根据时间,获取指定范围的数据集A, 再从数据集A 中,取出每行数据中的两个时间,卡另外一个B表里面的某列时间范围的值集.得到均值进行配置值比对. 这里有个问题,就是取得B ...
- Oracle_PL/SQL(3) 游标
引言:PLSQL数据类型标量数据类型:数字类.字符类.日期类.布尔类(boolean).复合数据类型:记录(%rowtype).表.数组引用类型:REF CURSORLOB类型:BLOB.CLOB 1 ...
- PL/SQL之游标的使用
Oracle中的游标有两种: 显式游标 用CURSOR...IS 命令定义的游标,它可以对查询语句(SELECT)返回的多条记录进行处理. 隐式游标 是在执行插入(INSERT).删除(DELETE) ...
- Oracle学习2 视图 索引 sql编程 游标 存储过程 存储函数 触发器
---视图 ---视图的概念:视图就是提供一个查询的窗口,来操作数据库中的数据,不存储数据,数据在表中. ---一个由查询语句定义的虚拟表. ---查询语句创建表 create table emp a ...
- SQL Server 游标的应用
----------------SQL游标应用----------------- 今天由于业务需求,需要在存储过程中实现有一个表的主键去匹配在另一个表中作为外键所对应的数值 ,若在C#中则非常简单只需 ...
- SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)
原文:SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database) 一.本文所涉及的内容(Contents) 本文所涉及的内容 ...
- SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database)
原文:SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database) 一.本文所涉及的内容(Contents) 本文所涉及的内容(C ...
- PL/SQL使用游标CURSOR
一.使用游标 对于DML语句和单行select into ,oracle自动分配隐形游标.处理select返回多行语句,可以使用显式游标. 使用显示游标处理多行数据,也可使用SELECT..BULK ...
随机推荐
- python标准模块(二)
本文会涉及到的模块: json.pickle urllib.Requests xml.etree configparser shutil.zipfile.tarfile 1. json & p ...
- 用css3实现各种图标效果(1)
公共样式 应该说现在绝大多数公司的项目前端都是一团乱,不仅仅是js写的没有任何框架而言,css同样也是如此,导致项目如果要升级或者说有新的变更维护起来就特别困难. 最近领导决定花大时间整理一下css样 ...
- 收集的一些jQuery (我平常用的少的,但确实挺有效果的)
禁用Jquery(动画)效果 jQuery.fx.off = true; 使用自己的 Bullets(这个有一丁点儿的小技巧) //这里是js代码 也就是给每个ul添加一个类名 然后给ul的子li前面 ...
- Nginx系列2之Nginx+php
preface 公司所有的大多数业务都泡在LNMP平台上,所以对PHP+Nginx有点了解,那么就做个小小的总结吧. what's FastCGi FastCGI是一个可伸缩,高速的在HTTP ser ...
- python 动态调用模块、类、方法(django项目)
需求:近一段时间基于django框架,开发各业务层监控代码,每个业务的监控逻辑不同,因此需要开发监控子模块,动态的导入调用. 项目名称:demo_django App:common_base.moni ...
- BZOJ2124: 等差子序列
题意:给一个 1 到 N 的排列{Ai},询问是否存在 1<=p1<p2<p3<p4<p5<…<pLen<=N(Len>=3),使得 Ap1,Ap ...
- photobooth.js jquery
<div id="example" class="photobooth" style="width:758px;height:400px&quo ...
- ajax请求下拉列表框的实现(面向对象封装类)
实现的效果图 <?php class Car{ private $carColor; private $carType; public function __construct($carColo ...
- JavaScript学习笔记——对象知识点
javascript对象的遍历.内存分布和封装特性 一.javascript对象遍历 1.javascript属性访问 对象.属性 对象[属性] //字符串格式 //javascript属性的访问方法 ...
- Unixbench测试工具和使用
安装过程 wget http://soft.laozuo.org/scripts/UnixBench5.1.3.tgz tar xf UnixBench5.1.3.tgz cd UnixBench5. ...