博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
游标+递归 查询 客户 子客户 查询财务信用
阅读量:4885 次
发布时间:2019-06-11

本文共 2890 字,大约阅读时间需要 9 分钟。

USE [CRM01]GO/****** Object:  StoredProcedure [dbo].[Account3YearsTrade]    Script Date: 07/15/2015 08:34:37 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- Batch submitted through debugger: SQLQuery1.sql|7|0|C:\Users\crmqas2\AppData\Local\Temp\3\~vs3E13.sqlALTER PROCEDURE  [dbo].[Account3YearsTrade] as DECLARE @AccountId UNIQUEIDENTIFIER ,    @yt_total_price money ,    @OpportunityId UNIQUEIDENTIFIER ,    @yt_invoice_date int ,    @yt_lastyeartrading money ,    @yt_toptwoannualturnover money ,    @yt_topthreeannualturnover money ,    @nowdate int     --声明一个游标mycursor,select语句中参数的个数必须要和从游标取出的变量名相同  DECLARE mycursor CURSORFOR    select accountid from accountBase WHERE StateCode=0     SELECT @nowdate=DATEPART(year,Getdate())--打开游标  OPEN mycursor          --从游标里取出数据赋值到我们刚才声明的2个变量中  FETCH NEXT FROM mycursor INTO @AccountId    --判断游标的状态      -- 0 fetch语句成功          ---1 fetch语句失败或此行不在结果集中          ---2 被提取的行不存在  	WHILE ( @@fetch_status = 0 ) 		BEGIN 			set @yt_lastyeartrading = 0			set @yt_toptwoannualturnover = 0			set @yt_topthreeannualturnover = 0			DECLARE mycursortwo CURSOR			FOR   			  WITH childAccount(accountid,ParentAccountId) as 			   ( 				SELECT accountid,ParentAccountId FROM AccountBase WHERE accountid=@AccountId				UNION ALL 				SELECT A.accountid,A.ParentAccountId FROM AccountBase A,childAccount b 				where a.ParentAccountId = b.accountid 			   )				select yt_total_price,DATEPART(year,yt_invoice_date),OpportunityId from Opportunity				where yt_arrive in(SELECT accountid  from childAccount)   and yt_status=100000002			    open mycursortwo    				FETCH NEXT FROM mycursortwo INTO @yt_total_price,@yt_invoice_date,@OpportunityId				while( @@fetch_status = 0 )					begin						if(@yt_invoice_date=@nowdate-1)							begin								set @yt_lastyeartrading = @yt_lastyeartrading + isnull(@yt_total_price,0.0000)							end						else if(@yt_invoice_date=@nowdate-2)							begin								set @yt_toptwoannualturnover = @yt_toptwoannualturnover + isnull(@yt_total_price,0.0000)							end						else if(@yt_invoice_date=@nowdate-3)							begin								set @yt_topthreeannualturnover = @yt_topthreeannualturnover + isnull(@yt_total_price,0.0000)							end				FETCH NEXT FROM mycursortwo  INTO @yt_total_price,@yt_invoice_date,@OpportunityId					end		   CLOSE mycursortwo		   DEALLOCATE mycursortwo		   update Account		   set yt_lastyeartrading = @yt_lastyeartrading,yt_lastyeartrading_Base = @yt_lastyeartrading,yt_toptwoannualturnover = @yt_toptwoannualturnover,			   yt_toptwoannualturnover_Base = @yt_toptwoannualturnover,yt_topthreeannualturnover = @yt_topthreeannualturnover,yt_topthreeannualturnover_Base = @yt_topthreeannualturnover		   where  AccountId = @AccountId		   		   --用游标去取下一条客户  		   FETCH NEXT FROM mycursor  INTO @AccountId		END      --关闭游标  CLOSE mycursor      --撤销游标  DEALLOCATE mycursor

  

转载于:https://www.cnblogs.com/zhaojingwei/p/4660414.html

你可能感兴趣的文章
Hadoop入门第五篇:Hive简介以及部署
查看>>
最大熵学习笔记(四)模型求解
查看>>
看源码
查看>>
《软件需求工程》读后感06
查看>>
javascript数组操作
查看>>
【mysql】mysql内置函数
查看>>
分析函数
查看>>
Spark学习笔记(一)
查看>>
虫食算
查看>>
10.从远程仓库中抓取与拉取
查看>>
在可编辑datagrid中,使用my97日期控件
查看>>
每日一小练——按字典顺序列出全部排列
查看>>
The 16th tip of DB Query Analyzer
查看>>
ftoa浮点型转换成字符串
查看>>
rabbitMQ学习(六)
查看>>
迅为4412开发板学习之win8下基础软件的安装和学习
查看>>
初识web2py
查看>>
script & scriptreplay
查看>>
Docker最全教程——从理论到实战(二)
查看>>
HDU4109-instruction agreement(差分约束-最长路+建立源点,汇点)
查看>>