22,210
社区成员
发帖
与我相关
我的任务
分享
定义变量部分
declare @Quantity numeric(19,6)
,@Docentry int
,@Quantity1 numeric(19,6)
declare GetSCSH cursor for select Docentry,QUANTITY,QUANTITY1 from #temp
open GetSCSH
fetch next from GetSCSH into Docentry,Quantity,@Quantity1
while @@fetch_status=0
begin
if(@Quantity<>0)
begin
update #temp set Quantity1=@Quantity1+@Quantity where Docentry=@Docentry
end
fetch next from GetSCSH into @Docentry,@Quantity,@Quantity1
end
close GetSCSH
deallocate GetSCSH
SELEVT * FROM #temp
declare @tmp int
update #temp
set @tmp=isnull(@tmp,0)+QUANTITY,QUANTITY1=@tmp
declare @Quantity numeric(19,6)
,@Docentry int
,@Quantity1 numeric(19,6)
SET @Quantity1 = 0
declare GetSCSH cursor for select Docentry,QUANTITY from #temp
open GetSCSH
fetch next from GetSCSH into @Docentry,@Quantity
while @@fetch_status=0
begin
if(@Quantity<>0)
begin
SET @Quantity1 = @Quantity1 + @Quantity
update #temp set Quantity1 = @Quantity1 where Docentry=@Docentry
end
fetch next from GetSCSH into @Docentry,@Quantity
end
close GetSCSH
deallocate GetSCSH
declare @Quantity numeric(19,6)
,@Docentry int
,@Quantity1 numeric(19,6)=0
declare GetSCSH cursor for select Docentry,QUANTITY from #temp
open GetSCSH
fetch next from GetSCSH into @Docentry,@Quantity
while @@fetch_status=0
begin
if(@Quantity<>0)
begin
update #temp set Quantity1=@Quantity1+@Quantity where Docentry=@Docentry
select @Quantity1=@Quantity1+@Quantity
end
fetch next from GetSCSH into @Docentry,@Quantity
end
close GetSCSH
deallocate GetSCSH
SELECT * FROM #temp
create table tb (Docentry bigint primary key,QUANTITY int)
insert tb select
3979 ,3 union
select 3981, 1 union
select 3980 ,1 union
select 3978, 2
select * from tb
/*
Docentry QUANTITY
3978 2
3979 3
3980 1
3981 1
*/
select Docentry ,QUANTITY ,(select sum(QUANTITY) from tb where Docentry<=a.Docentry) as QUANTITY1
from tb a
/*
Docentry QUANTITY QUANTITY1
3978 2 2
3979 3 5
3980 1 6
3981 1 7
*/
drop table tb
declare @Quantity numeric(19,6)
,@Docentry int
,@Quantity1 numeric(19,6)=0
declare GetSCSH cursor for select Docentry,QUANTITY from #temp
open GetSCSH
fetch next from GetSCSH into @Docentry,@Quantity
while @@fetch_status=0
begin
if(@Quantity<>0)
begin
update #temp set Quantity1=@Quantity1+@Quantity where Docentry=@Docentry
select @Quantity1=@Quantity1+@Quantity
end
fetch next from GetSCSH into @Docentry,@Quantity
end
close GetSCSH
deallocate GetSCSH
SELECT * FROM #temp
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2012-05-16 10:25:32
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([Docentry] int,[QUANTITY] int,[QUANTITY1] sql_variant)
insert [tb]
select 3979,3,null union all
select 3981,1,null union all
select 3980,1,null union all
select 3978,2,null
--------------开始查询--------------------------
;with f as
(
select px=row_number()over(order by getdate()),* from tb
)
select
Docentry, QUANTITY ,
(select sum(QUANTITY) from f where px<=t.px) as QUANTITY1
from
f t
----------------结果----------------------------
/* Docentry QUANTITY QUANTITY1
----------- ----------- -----------
3979 3 3
3981 1 4
3980 1 5
3978 2 7
(4 行受影响)
*/
select Docentry ,QUANTITY ,(select sum(QUANTITY) from #temp b where b.Docentry<=a.Docentry) as QUANTITY1
from #temp a
为什么要用游标?
直接
select
Docentry, QUANTITY ,
(select sum(QUANTITY) from tb where Docentry<=t.Docentry)
from
tb t