sql语句双重循环
来源:互联网 发布:java虚拟机怎么算 编辑:程序博客网 时间:2024/05/01 12:13
表 j_wenzhang_aps201503 中 shunxu 字段为null。现在 想根据 lanmu_id,qishiye两项,更新shunxu 字段。
1 、如果让shunxu 字段 自增,不存在重复,且lanmu_id 较小的,对应的 shunxu 也小;lanmu_id 相同,qishiye较小的,对应的shunxu也小。
declare @maxid int declare @minid intdeclare @shunxu intdeclare @maxqishiye intdeclare @minqishiye intselect @maxid= max(lanmu_id) from j_wenzhang_aps201503 where shunxu is null select @minid= min(lanmu_id) from j_wenzhang_aps201503 where shunxu is null select @shunxu=0if ((@maxid is not null) and (@minid is not null))beginwhile (@maxid>=@minid) beginselect @maxqishiye=max(qishiye) from j_wenzhang_aps201503 where shunxu is null and lanmu_id=@minidselect @minqishiye=min(qishiye) from j_wenzhang_aps201503 where shunxu is null and lanmu_id=@minidif ((@maxqishiye is not null) and (@minqishiye is not null))beginwhile (@maxqishiye>=@minqishiye) beginselect @shunxu=@shunxu+1update j_wenzhang_aps201503 set shunxu=@shunxu where lanmu_id=@minid and qishiye=@minqishiye and shunxu is nullselect @minqishiye=min(qishiye) from j_wenzhang_aps201503 where shunxu is null and lanmu_id=@minidendendselect @minid=min(lanmu_id) from j_wenzhang_aps201503 where shunxu is null endendgo
执行后结果:
2、如果按照栏目 让shunxu 字段 自增,且lanmu_id 较小的,对应的 shunxu 也小;lanmu_id 相同,qishiye较小的,对应的shunxu也小。
即(每个相同lanm_id里,shunxu 都从1 开始增加)
declare @maxid int declare @minid intdeclare @shunxu intdeclare @maxqishiye intdeclare @minqishiye intselect @maxid= max(lanmu_id) from j_wenzhang_aps201503 where shunxu is null select @minid= min(lanmu_id) from j_wenzhang_aps201503 where shunxu is null if ((@maxid is not null) and (@minid is not null))beginwhile (@maxid>=@minid) beginselect @maxqishiye=max(qishiye) from j_wenzhang_aps201503 where shunxu is null and lanmu_id=@minidselect @minqishiye=min(qishiye) from j_wenzhang_aps201503 where shunxu is null and lanmu_id=@minidselect @shunxu=0if ((@maxqishiye is not null) and (@minqishiye is not null))beginwhile (@maxqishiye>=@minqishiye) beginselect @shunxu=@shunxu+1update j_wenzhang_aps201503 set shunxu=@shunxu where lanmu_id=@minid and qishiye=@minqishiye and shunxu is nullselect @minqishiye=min(qishiye) from j_wenzhang_aps201503 where shunxu is null and lanmu_id=@minidendendselect @minid=min(lanmu_id) from j_wenzhang_aps201503 where shunxu is null endendgo
执行后结果:
0 0
- sql语句双重循环
- 10、SQL Server:sql语句双重循环
- for语句双重循环
- 循环语句及双重循环
- 双重循环sql操作
- SQL 双重条件查询语句
- 队伍分组统计 (双重循环遍历出来)或 (sql语句查出来
- 双重for循环语句块的优化
- 一个简单的双重for循环的汇编语句解释
- SQL循环语句
- sql,循环语句
- sql 循环语句
- SQL sever 循环语句
- SQL循环语句
- SQL中循环语句
- sql 循环语句
- SQL 循环语句
- SQL循环语句
- RecyclerView 定制item 分割线
- 进制转换公式 及 浮点数内存存储备忘
- Unoidl中的关键字
- 3D打印技术之切片引擎(1)
- shell 之 awk
- sql语句双重循环
- java计算期末平均分的工具源代码
- sd integrate
- Object类中的方法介绍
- 我用时间称自己的重量,然后数落自己的肤浅与狂妄——2014年终总结
- Sonar + IOS 安装失败。
- 兼容excel2003以及excel2007导入和导出
- Use gitk to understand git
- 聊聊并发——生产者消费者模式