各位大侠,请问怎样在存储过程里,批量更新数据啊?(刚才我问题 我描述错了,不好意思,只好再提问一次)

打酱油的无证程序猿 2010-01-19 10:57:46

我有一个表

MyTab
----------
ID ClassType OrderNum
1 2 1
2 3 3
2 3 5



我想写一个存储过程 Inc_OrderNum,该存储过程有一个@ClassType参数,
调用 exec Inc_OrderNum 3 之后,
对OrderNum字段批量更新 ClassType =3的记录,即 符号条件的每条记录的OrderNum 字段要重新编码

ClassType =3 的记录有两条,先让这些记录按现有的OrderNum排序,然后第2条记录改OrderNum=1,第2条记录改OrderNum=2


MyTab
----------
ID ClassType OrderNum
1 2 1
2 3 1 //3->1
2 3 2 //5->2





CREATE PROCEDURE Inc_OrderNum
(
@ClassType int
)
AS

--请问这个存储过程怎么写呢?,谢谢各位大侠!!!


GO

...全文
243 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
yananguo_1985 2010-01-19
  • 打赏
  • 举报
回复

1 2 1
2 3 3
2 3 5
2 3 2

更新第一个3时候,由于select count(1)+1 from test where ClassType=@ClassType and OrderNum<a.OrderNum(select count(1)+1 from test where ClassType=3 and OrderNum<3)=2
则更新3=>2,
更新5时候,(select count(1)+1 from test where ClassType=3 and OrderNum<5)=1
则更新3=>1
以下类似。。
  • 打赏
  • 举报
回复
ok,多谢SQL77大侠!!!
SQL77 2010-01-19
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 cnlmgsoft 的回复:]
nianran520大侠,用变量循环更新不一定好的,是什么意思啊?


update  select count(1)+1  嵌套子查询的方法 为什么就好呢?


nianran520大侠,快来帮忙啊!



[/Quote]
晕,循环更新只能从上到下呀,而子查询可以排好序更新
  • 打赏
  • 举报
回复
ok,多谢各位大侠,结贴了,非常感谢!!!

SQL太博大精深了!
nianran520 2010-01-19
  • 打赏
  • 举报
回复
变量循环更新是按物理排序逐个更新
(select count(1) from MyTab where OrderNum<=t.OrderNum and ClassType=t.ClassType)
是统计小于等于当前值的个数
yananguo_1985 2010-01-19
  • 打赏
  • 举报
回复

if object_id('test') is not null drop table test
create table test([ID] int,[ClassType] int,[OrderNum] int)
insert test
select 1,2,1 union all
select 2,3,3 union all
select 2,3,5 union all
select 2,3,2

select * from test
--------------------
1 2 1
2 3 3
2 3 5
2 3 2


create PROCEDURE Inc_OrderNum
(
@ClassType int
)
AS
begin
update test
set OrderNum=
(select count(1)+1 from test where ClassType=@ClassType and OrderNum<a.OrderNum) from test a
where
ClassType=@ClassType
end

-------------------
exec Inc_OrderNum 3
--------------------
1 2 1
2 3 2
2 3 3
2 3 1

  • 打赏
  • 举报
回复
nianran520大侠,用变量循环更新不一定好的,是什么意思啊?


update select count(1)+1 嵌套子查询的方法 为什么就好呢?


nianran520大侠,快来帮忙啊!


nianran520 2010-01-19
  • 打赏
  • 举报
回复
--这样的话只能用
create proc Inc_OrderNum @ClassType int
as
begin
update t
set OrderNum = (select count(1) from MyTab where OrderNum<=t.OrderNum and ClassType=t.ClassType)
from MyTab t
where ClassType=@ClassType
end
--或者用临时表
SQL77 2010-01-19
  • 打赏
  • 举报
回复
select count(1) from test where ClassType=@ClassType and OrderNum<=a.OrderNum

COUNT(1)就是增加了个排序列
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 fredrickhu 的回复:]
引用 11 楼 cnlmgsoft 的回复:
nianran520大侠的答案感觉很棒!!! 简洁明了!

上面几位大侠的代码都看不懂!select count(1)+1 看的郁闷


nianran520大侠能不能加上排序啊???谢谢啊!!!
因为我想先 对符号条件的记录 按现有的OrderNum排序,然后第1条记录改OrderNum=1,第2条记录改OrderNum=2

这种用变量循环更新不一定好的 还是用count(1)+1吧
[/Quote]

count(1)+1 是什么意思啊???谢谢为什么要用count(1)?
  • 打赏
  • 举报
回复
原表
MyTab
----------
ID ClassType OrderNum
1 2 1
2 3 3
3 3 5
4 3 2

调用存储过程之后的,我想得到的结果
MyTab
----------
ID ClassType OrderNum
1 2 1
2 3 2 //3->2
3 3 3 //5->3
4 3 1 //2->1
yananguo_1985 2010-01-19
  • 打赏
  • 举报
回复

if object_id('test') is not null drop table test
create table test([ID] int,[ClassType] int,[OrderNum] int)
insert test
select 1,2,1 union all
select 2,3,3 union all
select 2,3,4

select * from test


create PROCEDURE Inc_OrderNum
(
@ClassType int
)
AS
begin
update test
set OrderNum=
(select count(1)+1 from test where ClassType=@ClassType and OrderNum<a.OrderNum) from test a
where
ClassType=@ClassType
end

exec Inc_OrderNum 3

---------------------
1 2 1
2 3 1
2 3 2
  • 打赏
  • 举报
回复
原表
MyTab
----------
ID ClassType OrderNum
1 2 1
2 3 3
3 3 5
4 3 2

调用存储过程之后的,我想得到的结果
MyTab
----------
ID ClassType OrderNum
1 2 1
2 3 2 //3->2
3 3 3 //5->3
4 3 1 //2->1

--小F-- 2010-01-19
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 cnlmgsoft 的回复:]
nianran520大侠的答案感觉很棒!!! 简洁明了!

上面几位大侠的代码都看不懂!select count(1)+1 看的郁闷


nianran520大侠能不能加上排序啊???谢谢啊!!!
因为我想先 对符号条件的记录 按现有的OrderNum排序,然后第1条记录改OrderNum=1,第2条记录改OrderNum=2
[/Quote]
这种用变量循环更新不一定好的 还是用count(1)+1吧
  • 打赏
  • 举报
回复
原表
MyTab
----------
ID ClassType OrderNum
1 2 1
2 3 3
3 3 5
4 3 2

调用存储过程之后的,我想得到的结果
MyTab
----------
ID ClassType OrderNum
1 2 1
2 3 2 //3->2
3 3 5 //5->3
4 3 2 //2->1


  • 打赏
  • 举报
回复
nianran520大侠的答案感觉很棒!!! 简洁明了!

上面几位大侠的代码都看不懂!select count(1)+1 看的郁闷


nianran520大侠能不能加上排序啊???谢谢啊!!!
因为我想先 对符号条件的记录 按现有的OrderNum排序,然后第1条记录改OrderNum=1,第2条记录改OrderNum=2
nianran520 2010-01-19
  • 打赏
  • 举报
回复
if object_id('[MyTab]') is not null drop table [MyTab]
create table [MyTab]([ID] int,[ClassType] int,[OrderNum] int)
insert [MyTab]
select 1,2,1 union all
select 2,3,3 union all
select 2,3,5
--存储过程
create proc Inc_OrderNum @ClassType int
as
begin
declare @i int
select @i = 0
update MyTab
set OrderNum = @i,
@i=@i+1
where ClassType=@ClassType
end
--执行
exec Inc_OrderNum 3
--结果
select * from [MyTab]
--------------------
1 2 1
2 3 1
2 3 2

nianran520 2010-01-19
  • 打赏
  • 举报
回复
create proc Inc_OrderNum @ClassType int
as
begin
declare @i int
select @i = 0
update MyTab
set OrderNum = @i,
@i=@i+1
where ClassType=@ClassType
end
Mr_Nice 2010-01-19
  • 打赏
  • 举报
回复

update A
set OrderNum=(select count(1)+1 from MyTab where ClassType=@ClassType
and OrderNum < A.OrderNum)
from MyTab A
where ClassType=@ClassType

都好快啊!
playwarcraft 2010-01-19
  • 打赏
  • 举报
回复
sql2005 直接row_number() over (partion by Classtype order by getdate())
加载更多回复(6)

34,575

社区成员

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

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