存储过程更新一个表的所有记录

来源:百度知道 编辑:UC知道 时间:2024/06/10 06:29:36
我想在一个存储过程里更新一个表的所有记录的,这个表中有一个字段叫做“CR”这个是INT型的,在前台传过来两个数分别是,@BEGIN和@END我想生成一个@BEGIN和@END之间的随机数,然后在把这个随机数和表中CR这个字段的相加,而且随机数要求循环生成,要求每条记录要加的随机数都不能一样。在线等,哪位高手帮我写一下谢了。

GO

--创建存储过程

CREATE PROCEDURE PRO_Biaoming_IN

@BEGIN int, --定义参数

@END int,

AS

create table #temp
(
fid int identity,
ID int,
CR int
)

insert into #temp
select ID,CR from biaoming

declare @sum int
set @sum=(select count(ID) from #temp)

while @sum>0
begin
update biaoming set CR=CR+t.fnum
from (select ID,cast(RAND()*100 as int) fnum from #temp
where ID=(select ID from #temp where fid=@sum) )t
where t.ID=biaoming.ID and t.fnum>=20 and t.fnum<=50
set @sum=@sum-1
end

drop table #temp
GO

--仅假设lz说的CR字段的表的名字叫做biaoming,ID为该表的主键