这样存储过程这么写?急!!!!

myschevon 2004-06-14 11:42:56
表结构如下:
table 1 table2

p_id n_id type number n_id t1 t2 t3 t4

1 xy1 t1 1 xy1 1 0 1 0
2 xy1 t2 2 xy2 1 1 1 1
1 xy2 t1 3 xy3 1 1 1 1
3 xy2 t4 4 xy4 1 1 1 1


select * from table1 where p_id=1
if 查询结果不为空
begin
while(查询结果的行数)
begin
if type=t1
update table2 set t1=t1-从table1中查询出的number字段 where n_id=从table1中查询出的n_id
if type=t2
update table2 set t2=t2-从table1中查询出的number字段 where n_id=从table1中查询出的n_id
if type=t3
update table2 set t2=t3-从table1中查询出的number字段 where n_id=从table1中查询出的n_id
if type=t4
update table2 set t2=t4-从table1中查询出的number字段 where n_id=从table1中查询出的n_id
end
end
...全文
167 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yemao20 2004-06-14
  • 打赏
  • 举报
回复
如上.
UP
anbeel 2004-06-14
  • 打赏
  • 举报
回复
用游标:

declare mycur for select * from table1 where p_id=1

open mycur

Fetch Next from mycur into 变量

while @@fetch_status=0
begin
if (..)
begin
处理。。。
end
Fetch Next from mycur into 变量
end

Close mycur
Deallocate mycur

13880079673 2004-06-14
  • 打赏
  • 举报
回复
declare mycur for select type from table1 where p_id=1
open mycur
Fetch Next from mycur into @count
while @@fetch_status=0
begin
if type=t1
update table2 set table2.t1 = table2.t1-table1.number from table1,table2 where table2.n_id = table1.n_id
if type=t2
update table2 set table2.t2 = table2.t2-table1.number from table1,table2 where table2.n_id = table1.n_id
if type=t3
update table2 set table2.t2 = table2.t3-table1.number from table1,table2 where table2.n_id = table1.n_id
if type=t4
update table2 set table2.t2 = table2.t4-table1.number from table1,table2 where table2.n_id = table1.n_id
Fetch Next from mycur into 变量
end

Close mycur
Deallocate mycur

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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