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 ...
随机推荐
- Baccarat中挖矿、兑换和做市的三角关系是什么?
NGK在这波DeFi潮中,推出了Baccarat,为用户带来了流动性挖矿收益,今天笔者就讲一讲Baccarat中挖矿.兑换和做市的关系. 兑换和做市是什么关系呢?众所周知,换币者,是用一种货币去换另一 ...
- C++算法代码——快速排序
题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?cid=1371&pid=1 题目描述 第一行一个数字n, n<=100000. ...
- Elasticsearch CRUD基本操作
前言 本次我们聊一聊Elasticsearch的基本操作CRUD,他跟我们常用的关系型数据库的操作又有什么不一样的地方呢?今天我们就来好好讲解一番. 说明 本次演示用的版本是7.11. 工具可以使用K ...
- JAVA 判断一个字符串是否是合法的日期格式?
采用SimpleDateFormat类的parse方法进行判断,如果转换不成功,就会出现异常.另外,还需要判断字符串的长度,若不判断,第二个字符串就会验证通过,实际上也不是合法的.话不多说,且看代码: ...
- Linux安装jdk(两种方式)
最近在研究大数据方面的东西,业务场景是从设备采集数据经过处理然后存放DB. 建设上面的环境第一步肯定是安装jdk,所以和大家一起学一下基本知识centos7.5安装jdk1.8. 安装jdk有两种方法 ...
- 一文了解python的 @property
参考自: https://www.programiz.com/python-programming/property Python为我们提供了一个内置装饰器@property,此方法使得getter和 ...
- go 报错 import cycle not allowed
运行时报错,import cycle not allowed : 查了goole大概知道了原因,还是导包类的问题,我检察了一下我的代码库,发现我昨天划分几个工具文件,里面的两个文件相互引用,就导致报i ...
- 剑指 Offer 09. 用两个栈实现队列 +java中栈和队列的使用
剑指 Offer 09. 用两个栈实现队列 题目链接 class CQueue { private Stack<Integer> sta1; private Stack<Intege ...
- Linux速通 大纲
1.Linux操作系统安装及初始化配置(熟悉) 2.Linux操作系统目录组成结构及文件级增删改查操作(重点) 3.Linux操作系统用户.权限管理(重点) 4.开源软件及Linux下软件包的管理(重 ...
- linux 设置系统时间
第一种: 服务器date时间不准: root@mdy-zabbix2:~# date Fri Sep 28 09:58:56 UTC 2018 实际是下午6点 第一步:执行tzselect 第二步: ...