Sql Server存储过程和游标的配合操作
本段代码主要为了记录存储过程以及游标的使用,防止以后自己忘记
知识点:1、存储过程书写
2、游标书写
3、游标循环更新记录
create proc saletarget
as
declare @oweridname varchar(20)
declare @hmp_fiscalyear int
declare @hmp_businessunitidname varchar(20)
declare @hmp_nationterritoryidname varchar(20)
declare @hmp_currentyeartargetwithvat decimal(18,2)
declare @hmp_month int
declare @hmp_taxtargetamount decimal(18,2)
declare @quarters varchar(10)
declare @ac decimal(18,2)
declare @saleach decimal(10,2)
declare @yaact decimal(18,2)
declare @growth decimal(10,2)
declare @hmp_salestargetdetailId varchar(50)
begin
select * into #temp from(select A.OwnerIdName,A.hmp_fiscalyear,hmp_BusinessUnitIdName,hmp_nationterritoryidName,isnull(hmp_currentyeartargetwithvat,0) as hmp_currentyeartargetwithvat,hmp_month,isnull(hmp_taxtargetamount,0) as hmp_taxtargetamount,cast('' as varchar(10)) as quarters,cast(0 as decimal(18,2)) as ac,cast(0 as decimal(10,2)) as saleach,cast(0 as decimal(18,2)) as yaact,cast(0 as decimal(10,2)) as growth,b.hmp_salestargetdetailId from hmp_target A INNER JOIN hmp_salestargetdetail B ON A.hmp_targetId = B.hmp_targetid where hmp_month is not null)a
DECLARE mycursor CURSOR
FOR
select * from #temp
open mycursor
fetch next from mycursor into @oweridname,@hmp_fiscalyear,@hmp_businessunitidname,@hmp_nationterritoryidname,@hmp_currentyeartargetwithvat,@hmp_month,@hmp_taxtargetamount,@quarters,@ac,@saleach,@yaact,@growth,@hmp_salestargetdetailId
--判断游标的状态
-- 0 fetch语句成功
---1 fetch语句失败或此行不在结果集中
---2 被提取的行不存在
WHILE (@@fetch_status = 0)
begin
--获取本月完成数
select @ac = SUM(hmp_totaltaxprices) from hmp_deliverylistorder where hmp_SystemUseridName = @oweridname and hmp_deliverydate>=dateadd(month, datediff(month, 0, @hmp_fiscalyear+'-'+@hmp_month+'-01'), 0) and hmp_deliverydate<=dateadd(month, datediff(month, 0, dateadd(month, 1, @hmp_fiscalyear+'-'+@hmp_month+'-01')), -1)
--获取去年同期完成数
select @yaact = SUM(hmp_totaltaxprices) from hmp_deliverylistorder where hmp_SystemUseridName = @oweridname and hmp_deliverydate>=dateadd(yy,-1,dateadd(month, datediff(month, 0, @hmp_fiscalyear+'-'+@hmp_month+'-01'), 0)) and hmp_deliverydate<=dateadd(yy,-1,dateadd(month, datediff(month, 0, dateadd(month, 1, @hmp_fiscalyear+'-'+@hmp_month+'-01')), -1))
--目标完成率
if @hmp_taxtargetamount>0
set @saleach = @ac/@hmp_taxtargetamount*100
else
set @saleach = 0
--增长率
if @yaact>0
set @growth = (@ac-@yaact)/@yaact*100
else
set @growth = 0
update #temp set ac=@ac,saleach=@saleach,yaact=@yaact,growth=@growth where hmp_salestargetdetailId=@hmp_salestargetdetailId
fetch next from mycursor into @oweridname,@hmp_fiscalyear,@hmp_businessunitidname,@hmp_nationterritoryidname,@hmp_currentyeartargetwithvat,@hmp_month,@hmp_taxtargetamount,@quarters,@ac,@saleach,@yaact,@growth,@hmp_salestargetdetailId
end
close mycursor
deallocate mycursor
select * from #temp
end
Sql Server存储过程和游标的配合操作的更多相关文章
- sql server 存储过程使用游标记录
sql server 存储过程使用游标记录--方便下次参考使用 游标的组成: 声明游标 打卡游标 从一个游标中查找信息 关闭游标 释放游标 游标类型: 静态游标 动态游标 只进游标 键集驱动游标 静态 ...
- SQL Server存储过程和游标有关实例以及相关网址
内含游标的存储过程实例 第一种写法 GO BEGIN IF (object_id('PT_FAULT_REPORT', 'P') is not null) drop proc PT_FAULT_REP ...
- SQL Server基础之游标
查询语句可能返回多条记录,如果数据量非常大,需要使用游标来逐条读取查询结果集中的记录.应用程序可以根据需要滚动或浏览其中的数据.本篇介绍游标的概念.分类.以及基本操作等内容. 一:认识游标 游标是 ...
- SQL Server存储过程Return、output参数及使用技巧
SQL Server目前正日益成为WindowNT操作系统上面最为重要的一种数据库管理系统,随着 SQL Server2000的推出,微软的这种数据库服务系统真正地实现了在WindowsNT/2000 ...
- SQL Server 存储过程(转载)
SQL Server 存储过程 Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这 ...
- (摘录)SQL Server 存储过程
文章摘录:http://www.cnblogs.com/hoojo/archive/2011/07/19/2110862.html SQL Server 存储过程 Transact-SQL中的存储过程 ...
- (4.26)sql server存储过程优化
此博客介绍了简单但有用的提示和优化,以提高存储过程的性能. 0.with recompile:重编译 exec uspGetSalesInfoForDateRange ‘1/1/2009’, 31/1 ...
- SQL Server 存储过程具体解释
SQL Server 存储过程具体解释 存储过程的优缺点 ◆长处: 运行速度更快. 存储过程仅仅在创造时进行编译,而一般SQL语句每运行一次就编译一次,所以使用存储过程运行速度更快. 存储过程用于处理 ...
- sql server 存储过程 output 和return的使用 方法,详解
SQL Server目前正日益成为WindowNT操作系统上面最为重要的一种数据库管理系统,随着 SQL Server2000的推出,微软的这种数据库服务系统真正地实现了在WindowsNT/2000 ...
随机推荐
- how to change svg polygon size by update it's points in js
how to change svg polygon size by update it's points in js matrixTransform https://stackoverflow.com ...
- Flutter 获取本地图片并剪切
安装依赖 dependencies: ... image_picker: image_cropper android\app\src\main\AndroidManifest.xml 将UCropAc ...
- 以太坊手续费上涨,矿工出逃,VAST前景向好!
根据最新数据显示,以太坊的Gas费用在最近几天大幅飙涨,尤其是在过去2小时内,增幅约20%,一度达到了17.67美元.而这也导致了,许多基于以太坊协议的相关项目无法被生态建设者使用,很多矿工也纷纷出逃 ...
- Markdown简单语法的使用
Markdown简单语法的使用 目录 Markdown简单语法的使用 前言 标题的设置 字体的设置 1.字体加粗 2.斜体 3.字体加粗斜体 3.删除线 引用 列表的使用 插入图片 分割线 代码的书写 ...
- [转]ROS中使用message_filters进行多传感器消息同步
转:http://www.rosclub.cn/post-1030.html 最近实验室老师在做一个多传感器数据采集实验,涉及到了消息同步.所以就学习了ROS官网下的消息同步工具message_fil ...
- Mybites学习
参考链接:https://www.cnblogs.com/dongying/p/4073259.html <select <!-- 1. id (必须配置) id是命名空间中的唯一标识符, ...
- 控制流程-if/while/for
目录 一.控制流程之if判断 1.单分支结构 2.双分支结构 3.多分支结构 二.控制流程之while循环 1.基本使用 2.break 3.continue 三.流程控制之for循环 1.break ...
- GDB调试:从入门到入土
GDB是类Unix操作糸统下使用命令行调试的调试软件,全名GNU Debugger,在NOI系列竞赛使用的NOI Linux系统中起很大作用(如果不想用毒瘤Guide或直接输出)(XXX为文件名) 1 ...
- MySQL提权 通过UDF
目录 UDF是什么 命令执行 文本写入 Example: 远程写入 反弹Shell 提权 UDF是什么 参考:https://www.cnblogs.com/litlife/p/9030673.htm ...
- 项目管理之Git
@[TOC]( Git命令:分支与合并)Git一款很好的项目版本管理工具,更是一款优秀的分布式项目管理工具.今天主要给大家介绍Git 强大的分支和合并功能,分支和合并可以说在实际的工作当中用到的是最多 ...