急!求一sql语句!!!!!!!!!!!!!

liaoxiaohua1981 2005-11-03 10:57:45
现有一个表A 里面有一个字段S 我想根据S的值的大小来,改变每个S的值
比如:把满足S>500 and S<600 条件的每个记录的相应 S 的值减去500
这个语句该怎么写
...全文
100 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zoubsky 2005-11-03
  • 打赏
  • 举报
回复
--测试代码
declare @tb table(int int identity(1,1),s int)
insert @tb
select 400 union all
select 100 union all
select 550 union all
select 620 union all
select 123

select * from @tb
update @tb set s = s - 500 where s > 500 and s < 600
select * from @tb
/*
更新前的数据
nt s
----------- -----------
1 400
2 100
3 550
4 620
5 123

更新后的数据



int s
----------- -----------
1 400
2 100
3 50
4 620
5 123

(所影响的行数为 5 行)


*/
cosio 2005-11-03
  • 打赏
  • 举报
回复
写个函数,传值进来就可以
liaoxiaohua1981 2005-11-03
  • 打赏
  • 举报
回复
update a set s=s-500 where S>500 and S<600
楼上的,这个"s-500" 中的S的值能确定出来吗?如果能他是以什么为标准的?
raohui_1978 2005-11-03
  • 打赏
  • 举报
回复
update a set s=s-500 where S>500 and S<600
cosio 2005-11-03
  • 打赏
  • 举报
回复

drop table test
create TABLE test
(a int,b int, c int)

insert into test values('100','300','400')
insert into test values('200','500','400')
insert into test values('300','600','400')
insert into test values('400','700','400')
insert into test values('500','600','400')
insert into test values('600','800','400')



create procedure Hd_SP_test(
@x int , @y int)
AS

declare @a int, @b int

declare TempCur scroll cursor
for
select a,b from test

open tempcur
fetch first from tempcur into @a, @b

while @@fetch_status =0
begin
if (@b>@x) and (@b<@y)
update test set b=@b-@x where a=@a
fetch next from tempcur into @a, @b
end

close tempcur
deallocate tempcur

exec hd_sp_test 500,800



______________________________________________

(所影响的行数为 1 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)
100 300 400
200 500 400
300 100 400
400 200 400
500 100 400
600 800 400




22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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