----------------SQL游标应用-----------------
if object_id('tempdb..#test0001') is not null drop table #test0001---------物料临时表
create table #test0001(ItemCode nvarchar(30)----物料代码
            ,SL DEC(19,3)----物料所需数量
            ,RowsID Bigint----用于循环物料
            )
if object_id('tempdb..#test0002') is not null drop table #test0002---------容器临时表
create table #test0002(Carton nvarchar(30)----容器代码
            ,ItemCode nvarchar(30)----物料代码
            ,CSL nvarchar(30)----容器里物料数量
            ,RowsID Bigint ----用于循环容器,相同物料不同的容器进行排序
            )
if object_id('tempdb..#test0003') is not null drop table #test0003---物料在各个容器中最终要的数量表
create table #test0003(ItemCode nvarchar(30)
            ,CarTon nvarchar(30)
            ,LastSL DEC(19,3)---在容器上所得数量
            )

insert into #test0001(ItemCode,SL,RowsID)
      values('A',5,1),('B',10,2)---添加物料A、B所需数量分别为5、10个,并给予物料排序

insert into #test0002(Carton,ItemCode,CSL,RowsID)
      values('Q','A',3,1),('T','A',5,2),('Q','B',5,1) ,('T','B',3,2),('S','B',5,3)---给容器添加物料以及数量并给予物料所在不同容器排序
      -----此时容器里的物料数为Q---A----3
      -------------------------T---A----5
      -------------------------Q---B----5
      -------------------------T---B----3
      -------------------------S---B----5

      -----则此时A物料在Q中获取3个、在T中取2个
      -----B物料在Q中取5个、在T中取3个、在S中取2个

declare @total_SL DEC(19,3)----临时物料总数量
declare @CT_SL DEC(19,3)----临时容器物料数量
declare @SL DEC(19,3)----临时物料数量

declare @test_cursor cursor------建立游标变量
declare @test_rowid bigint-------作为匹配游标变量

set @test_cursor = cursor for select RowsID from #test0001 ----设置游标绑定列

open @test_cursor--开启游标

fetch next from @test_cursor into @test_rowid--取第一个游标

while @@FETCH_STATUS = 0 ---一级循环
begin

  --循环主体
  select top 1
  @total_SL = temp01.SL
  from #test0001 temp01
  where temp01.RowsID = @test_rowid

  declare @test_cursor2 cursor
  declare @test_rowid2 bigint

  set @test_cursor2 = cursor for select temp02.RowsID --设置游标绑定列;此游标是在容器表中按相同物料不同容器进行排序的
  from #test0002 temp02
  inner join #test0001 temp01 on temp01.ItemCode=temp02.ItemCode
  where temp01.RowsID=@test_rowid

  --开启游标
  open @test_cursor2
  --取第一个游标
  fetch next from @test_cursor2 into @test_rowid2

  while @@FETCH_STATUS = 0 ------二级循环
  begin
    select top 1
    @CT_SL = temp02.CSL
    from #test0002 temp02
    inner join #test0001 temp01 on temp01.ItemCode=temp02.ItemCode
    where temp02.RowsID = @test_rowid2
    and temp01.RowsID = @test_rowid

    IF @CT_SL<@total_SL
    begin -----若容器中存货量小于物料总数量则取完容器内数量
    set @SL = @CT_SL
    set @total_SL = @total_SL-@SL
  end
  else
  begin
    set @SL = @total_SL
    set @total_SL = 0
  end

  -------------将循环每条记录插入#test0003表中-----------
  insert into #test0003(ItemCode
             ,CarTon
             ,LastSL
             )
        select temp01.ItemCode
            ,temp02.Carton
            ,@SL
        from #test0001 temp01
        inner join #test0002 temp02 on temp02.ItemCode=temp01.ItemCode
        where temp01.RowsID=@test_rowid
           and temp02.RowsID=@test_rowid2

        if @total_SL = 0 break;
        --循环取游标下一个值
        fetch next from @test_cursor2 into @test_rowid2
      end
      close @test_cursor2 --关闭二级游标
      deallocate @test_cursor2 --关闭二级游标

      --循环取游标下一个值
      fetch next from @test_cursor into @test_rowid
end
close @test_cursor--关闭一级游标
deallocate @test_cursor--关闭一级游标

结果:

select * from #test0001;

select * from #test0002;

select * from #test0003;

总结:
SQL 的游标类似于C程序里的for循环,上面的例子相当于C程序中的冒泡排序,SQL游标适用于存储过程中一张表需要循环匹配另一张表时用游标比较方便。

SQL 游标的应用的更多相关文章

  1. sql 游标例子 根据一表的数据去筛选另一表的数据

    sql 游标例子 根据一表的数据去筛选另一表的数据 DECLARE @MID nvarchar(20)DECLARE @UTime datetime DECLARE @TBL_Temp table( ...

  2. sql 游标循环当中重新赋值

    sql 游标循环当中的变量必须重新赋值不然变量的值就是前次循环的值

  3. PL/SQL 游标 (实验七)

    PL/SQL 游标 emp.dept 目标表结构及数据 要求 基于部门表建立游标dept_cursor1,使用记录变量接收游标数据,输出部门表信息: 显示格式: 部 门 号: XXX 部门名称: XX ...

  4. PL/SQL游标详解

    刚打开游标的时候,是位于一个空行,要用fetch into 才能到第一行. 只是要注意用更新游标的时候,不能在游标期间commit. 否则会报ORA-01002: fetch out of seque ...

  5. 网上看到一份详细sql游标说明 《转载 https://www.cnblogs.com/xiongzaiqiren/p/sql-cursor.html》

     SQL游标(cursor)详细说明及内部循环使用示例 游标 游标(cursor)是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果.每个游标区都有一个名字,用户可以用SQL语句逐一从游标中获 ...

  6. Oracle PL/SQL游标

    游标的提出: SQL是面向集合的,其结果一般是集合量(多条记录),而PL/SQL的变量一本是标量,其一组变量异常一直只能存放一条记录.所以仅仅使用变量并不能完全满足SQL语句向应用程序输出数据的要求. ...

  7. PL/SQL 游标

    本随笔不是原创,只是学习笔记,用于加深记忆,原创地址PL/SQL --> 游标 一.游标的相关概念和特性 1.定义: 映射到结果集中的某一行的特定位置,类似与C语言中的指针.即通过游标方式定位到 ...

  8. Library Cache优化与SQL游标

    Library Cache主要用于存放SQL游标,而SQL游标最大化共享是Library Cache优化的重要途径,可以使SQL运行开销最低.性能最优. 1 SQL语句与父游标及子游标 在PL/SQL ...

  9. SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总

    SQL Server游标   转载自:http://www.cnblogs.com/knowledgesea/p/3699851.html. 什么是游标 结果集,结果集就是select查询之后返回的所 ...

随机推荐

  1. 开发mis系统的技术

    一.b/s架构 b/s架构:就broser/server,浏览器/服务器的说法.服务器端要运行tomcat,提供链接数据库服务供java代码读写数据,这个可以在eclipse中配置运行.浏览器则解释j ...

  2. elasticsearch-5.2在windows下的安装方法

    elasticsearch-5.2.1安装方法 1. 安装java 下载安装java jdk 1.7 以上 配置java环境变量 右击[我的电脑]---[属性]-----[高级系统设置]---[环境变 ...

  3. E. Devu and Flowers

    E. Devu and Flowers time limit per test 4 seconds memory limit per test 256 megabytes input standard ...

  4. win8 wifi开关显示关闭,且设置里面wifi开关显示灰色的解决办法

    只要从华硕官网下载驱动,电源管理驱动,安装下面显示的几个软件即可,然后重启电脑,即可看见wifi热点,另外设置里面的wifi开关也将显示正常(刚开始安装了个驱动人生根本没用,最后在华硕官网下载了个电源 ...

  5. java初级开发程序员(第三单元)

    1.if基本选择结构: 语法: if(条件){     //程序执行时,先判断条件.当结果为true(真)时,程序先执行大括号的代码块,再执行if结构(即{}部分)后面的代码.当结果为false(假) ...

  6. iptables禁止ping入

    iptables禁止ping入 以下设置将允许自己往外ping 不允许别人ping自己 vi /etc/sysconfig/iptables 加入如下2条规则 -A INPUT -p icmp --i ...

  7. js五种设计模式说明与示例

    第一种模式:js工厂模式    var lev=function(){        return "啊打";      };      function Parent(){    ...

  8. mvc关于三级联动修改时数据回显

    在网上找了好久,都没有找到自己想要的那种效果,最后还是自己写出来了, 虽然方法有点笨. 这是Controller里 public ActionResult Edit(string id) { //查询 ...

  9. 在 JavaScript 中 prototype 和 __proto__ 有什么区别

    本文主要讲三个 问题 prototype 和 proto function 和 object new 到底发生了什么 prototype 和 proto 首先我们说下在 JS 中,常常让我们感到困惑的 ...

  10. Lowest Common Ancestor of a Binary Tree leetcode

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...