这样的存储过程如何写??????????????????

azsoft 2003-11-03 08:54:42
id sl ph
1 100 111
1 200 222
1 300 333
1 400 444
变量值=200 结果为
id sl ph
1 0 111
1 100 222
1 300 333
1 400 444
...全文
106 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
txlicenhe 2003-11-03
  • 打赏
  • 举报
回复
大力的更好,楼主结贴太快,让我赚到了。
pengdali 2003-11-03
  • 打赏
  • 举报
回复
declare @你的参数 int
set @你的参数=200


declare @i int
update 你的表 set @i=case when @你的参数>sl then 0 else sl-@你的参数 end,@你的参数=case when @你的参数>sl then @你的参数-sl else 0 end,sl=@i



一句话就可以了!!!楼主太心急了。
pengdali 2003-11-03
  • 打赏
  • 举报
回复
--测试:

create table 你的表(id int,sl int,ph int)
insert 你的表 values(1, 100 , 111)
insert 你的表 values(1 , 200 , 222)
insert 你的表 values(1 , 300 , 333)
insert 你的表 values(1 , 400 , 444)

declare @你的参数 int
set @你的参数=200


declare @i int
update 你的表 set @i=case when @你的参数>sl then 0 else sl-@你的参数 end,@你的参数=case when @你的参数>sl then @你的参数-sl else 0 end,sl=@i


select * from 你的表
go
drop table 你的表



id sl ph
----------- ----------- -----------
1 0 111
1 100 222
1 300 333
1 400 444

(所影响的行数为 4 行)
LoveSQL 2003-11-03
  • 打赏
  • 举报
回复
没看清题意,上面的不对。
create procedure test @a int
as
declare @sl int,@ph char(3)
declare cur cursor for select sl,ph from t order by sl
open cur
fetch next from cur into @sl,@ph
while @@fetch_status = 0 and @a > 0
begin
if @sl >= @a
begin
update t set sl = sl - @a where ph = @ph
set @a = 0
end
else
begin
update t set sl = 0 where ph = @ph
set @a = @a - @sl
end
fetch next from cur into @sl,@ph
end
close cur
deallocate cur

select * from t order by sl
go
pengdali 2003-11-03
  • 打赏
  • 举报
回复
应该可以不用游标
LoveSQL 2003-11-03
  • 打赏
  • 举报
回复
create procedure test @a int
as
update yourtable set sl=0 where sl=@a
select * from yourtable order by sl
go
txlicenhe 2003-11-03
  • 打赏
  • 举报
回复
create table t (id int,sl int,ph char(3))
insert t select 1, 100 , '111'
union all select 1 , 200 , '222'
union all select 1 , 300 , '333'
union all select 1 , 400, '444'
go
create procedure test @a int
as
declare @sl int,@ph char(3)
declare cur cursor for select sl,ph from t
open cur
fetch next from cur into @sl,@ph
while @@fetch_status = 0 and @a > 0
begin
if @sl >= @a
begin
update t set sl = sl - @a where ph = @ph
set @a = 0
end
else
begin
update t set sl = 0 where ph = @ph
set @a = @a - @sl
end
fetch next from cur into @sl,@ph
end
close cur
deallocate cur
go
exec test 200
select * from t
id sl ph
----------- ----------- ----
1 0 111
1 100 222
1 300 333
1 400 444

(所影响的行数为 4 行)

txlicenhe 2003-11-03
  • 打赏
  • 举报
回复
create table t (id int,sl int,ph char(3))
insert t select 1, 100 , '111'
union all select 1 , 200 , '222'
union all select 1 , 300 , '333'
union all select 1 , 400, '444'

alter procedure test @a int
as
declare @sl int,@ph char(3)
declare cur cursor for select sl,ph from t
open cur
fetch next from cur into @sl,@ph
while @@fetch_status = 0 and @a > 0
begin
if @sl >= @a
begin
update t set sl = sl - @a where ph = @ph
set @a = 0
end
else
begin
update t set sl = 0 where ph = @ph
set @a = @a - @sl
end
fetch next from cur into @sl,@ph
end
close cur
deallocate cur

exec test 350
select * from t
id sl ph
----------- ----------- ----
1 0 111
1 0 222
1 250 333
1 400 444

(所影响的行数为 4 行)


azsoft 2003-11-03
  • 打赏
  • 举报
回复
变量减第一条记录 不足时自动减下一条
sunshareforever 2003-11-03
  • 打赏
  • 举报
回复
楼主是想怎么样啊!
zjcxc 元老 2003-11-03
  • 打赏
  • 举报
回复
没看出你想做什么?
txlicenhe 2003-11-03
  • 打赏
  • 举报
回复
如果是先进先出,可以参考这个:
http://expert.csdn.net/Expert/topic/2097/2097772.xml?temp=.6836206
txlicenhe 2003-11-03
  • 打赏
  • 举报
回复
是先进先出么?
txlicenhe 2003-11-03
  • 打赏
  • 举报
回复
想要怎样?

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧