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 ...
随机推荐
- 2021 NGK新机遇!---NGK生态所、星空计划双赛道爆发
2021年数字加密货币行业迎来几大发展机遇:1.比特币为首的数量加密资产正处于另类资产向数字黄金定位的历史性巨大发展机遇中,2.Defi等新项目异军提起,形成丰富的行业生态,将在未来对旧有的金融格局产 ...
- VAST重磅出击,NGK网络搜索量超越ETH!
Wechat指数中,NGK超越ETH,NGK搜索指数是157648点位,单日环比上涨11.95%,ETH搜索指数是115604点位,就连区块链标杆的BTC也仅仅只有171669点位,我们可清楚的看到N ...
- NGK是如何运用IPFS分布式存储的?
整个夏季,除了天气的火热,还有的火热莫过于IPFS挖矿这个领域了.IPFS的概念火热到,你可以看到到处都在卖IPFS矿机.那么,是什么原因导致IPFS这么火呢?在这之前,我们先了解一下什么是IPFS技 ...
- scrapy 运行逻辑
爬虫的步骤:发送请求获得响应→解析并提取数据→保存数据 我们沿用这个朴素的逻辑去理解scrapy 一.发送请求获得响应 1.爬虫发送请求request到引擎 2.引擎将请求request传递给调度器s ...
- Django Admin 配置和定制基本功能(基本二次开发配置)
一 列表显示页面 1. list_display,列表时,定制显示的列 @admin.register(models.UserInfo) class UserAdmin(admin.ModelAdm ...
- 生成pdf phantomjs
注:原创文件,转载请注明出处 使用phantomjs生成还原度比较高的pdf文件,理论上生成word也可以,因需求没有做这块要求,功课留给大家去做了. 下载 https://phantomjs.org ...
- Win32API使用技巧 -- 置顶应用
Win32提供了SetForegroundWindow方法可以将应用设置到前台并激活,但是在某些场景下,只调用该接口会返回0,即设置失败.比如如下场景: 当前前台应用为一个全屏的应用,非前台应用的进程 ...
- C++中tuple类型
tuple是C++11新标准里的类型.它是一个类似pair类型的模板.pair类型是每个成员变量各自可以是任意类型,但是只能有俩个成员,而tuple与pair不同的是它可以有任意数量的成员.但是每个确 ...
- 学习java之基础语法(三)
学习java之基础语法(三) java运算符 计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运 ...
- Dos常用命令整理
Dos常用命令整理 打开cmd的方法 开始菜单 -> 系统 -> 命令提示符 组合键Win+R打开运行 -> 输入cmd 在任意文件夹下Shift+鼠标右键 -> 在此处打开命 ...