代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
MSSQL
】
使用游标对查询结果循环
作者:
mcgtts
/ 发布于
2013/12/31
/
878
1set ANSI_NULLS ON 3set QUOTED_IDENTIFIER ON 5GO 7ALTER PROCEDURE [dbo].[GetProductPriceFromYear] @ProductID int, @Date Datetime 13AS 15BEGIN create table #temp1 19( id int IDENTITY(1,1) NOT NULL primary key, AddDate varchar(20), [datename] varchar(20), week varchar(20), ProductName varchar(20), ProductID varchar(20), LPrice float(20), HPrice float(20), APrice float(20), WeekLPrice float(20), WeekHPrice float(20), WeekAPrice float(20), WeekDayCount int 47) 51declare @tempDate varchar(20) 53declare @AddDate varchar(20) 55declare @datenam varchar(20) 57declare @week varchar(20) 59declare @ProductName varchar(20) 61declare @Product varchar(20) 63declare @LPrice varchar(20) 65declare @HPrice varchar(20) 67declare @APrice varchar(20) 69Declare CurPrice Cursor For select distinct AddDate=CONVERT(char(10),a.AddDate,20),datename(dw,a.AddDate) as [datename], 73datepart(ww,a.AddDate) as week, b.ProductName,a.ProductID,a.LPrice,a.HPrice,a.APrice 75from dbo.SmsPrice as a join dbo.SmsProduct as b 77on a.ProductID = b.ProductId where a.ProductID = @ProductID AND Datediff(yy,a.AddDate,@Date)=0 order by AddDate desc 79Open CurPrice 81Fetch from CurPrice into @AddDate,@datenam,@week,@ProductName,@Product,@LPrice,@HPrice,@APrice 83insert into #temp1(AddDate,[datename],week,ProductName,ProductID,LPrice,HPrice,APrice) values(@AddDate,@datenam,@week,@ProductName,@Product,@LPrice,@HPrice,@APrice) 89While @@FETCH_STATUS=0 BEGIN if(@tempDate!=@AddDate) insert into #temp1(AddDate,[datename],week,ProductName,ProductID,LPrice,HPrice,APrice) values(@AddDate,@datenam,@week,@ProductName,@Product,@LPrice,@HPrice,@APrice) set @tempDate = @AddDate Fetch from CurPrice into @AddDate,@datenam,@week,@ProductName,@Product,@LPrice,@HPrice,@APrice end Close CurPrice Deallocate CurPrice 113--insert into #temp1(AddDate,[datename],week,ProductName,ProductID,LPrice,HPrice,APrice) 115--select distinct AddDate=CONVERT(char(10),a.AddDate,20),datename(dw,a.AddDate) as [datename], 117--datepart(ww,a.AddDate) as week, b.ProductName,a.ProductID,a.LPrice,a.HPrice,a.APrice 119--from dbo.Price as a join dbo.SmsProduct as b 121--on a.ProductID = b.ProductId where a.ProductID = @ProductID AND Datediff(yy,a.AddDate,@Date)=0 order by AddDate desc 123declare @weekid varchar(20) 125declare @tempWeek2 varchar(20) 127declare @WeekLPrice float(20) 129declare @WeekHPrice float(20) 131declare @WeekAPrice float(20) 135Declare Cur Cursor For select distinct week from #temp1 139Open Cur Fetch Cur Into @weekid 143While @@FETCH_STATUS=0 BEGIN select @tempWeek2=week,@WeekLPrice=avg(CONVERT(float,LPrice)),@WeekHPrice=avg(CONVERT(float,HPrice)), @WeekAPrice=avg(CONVERT(float,APrice)) from #temp1 where week =@weekid group by week update #temp1 set WeekLPrice = @WeekLPrice,WeekHPrice=@WeekHPrice,WeekAPrice=@WeekAPrice, WeekDayCount=(select count(*) from #temp1 where week = @weekid) where week = @weekid Fetch Cur Into @weekid end Close Cur Deallocate cur 163select * from #temp1 165END 166
试试其它关键字
游标循环
同语言下
.
SQL查询 多列合并成一行用逗号隔开
.
查看存储过程修改时间,最近执行时间
.
设置手动批量删除数据库相关进程
.
获取某个表中特定字段的所有字符串形式
.
SQL 如何去除重复的字符串
.
怎么去掉一个字段中的重复数据
.
String 去除空格 回车 换行 水平制表符
.
SQL查询和替换含有回车,空格,TAB
.
SQL SERVER 查询每日新增用户数量、次留数量
.
判断两个字符串是否存在相同的内容
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
mcgtts
贡献的其它代码
(
6
)
.
实现在删除数据后,自增列的值连续
.
调用rar.exe解压一个rar文件到系统的临时目录
.
根据域名查ip
.
存储过程拆分字符串
.
使用游标对查询结果循环
.
python 日期时间常用操作
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3