SQL中用游标循环更新数据(原)

     在实际项目中,有时候会用到数据得更新,我最近就遇到了。我有一个产品表(product),其中有三个字段是外键对象的ID,可是现在需要做搜索了,所以存ID就不行了,要在产品的关键字里面存储这三个外键ID的Name属性,也就是要存储对象的名称而不是ID,这样便于抓取数据(本人使用的Lucene,对纯文本抓取比较强悍)。

    那么现在问题很清楚了,就是要逐条更行产品表里面的每一条记录。我的关键字的定义规格是:“产品名称,第一类型名称,第二类型名称,第三类型名称”。也就是说,需要对每一行进行这样一个操作:首页根据三个外键ID查询出对应的Name名称,然后把三个Name和产品名称根据规则连在一起,再更新这条数据的关键字列。

   好了,那思路很清楚了,做一件事,然后循环就可以了,那就要先做这件事,也就是创建一个存储过程,来完成第一步。

 

ExpandedBlockStart.gif View Code
 1  use 数据库名称
 2  go
 3  if  exists( select  *  from sysobjects  where name  =  ' updateKeyword ')
 4  drop  procedure updateKeyword
 5  go
 6  create  proc updateKeyword
 7  @productID  varchar( 50)
 8 
 9  as
10  declare  @typeID  varchar( 50)
11  declare  @typeID2  varchar( 50)
12  declare  @typeID3  varchar( 50)
13  declare  @typeName  varchar( 50)
14  declare  @typeName2  varchar( 50)
15  declare  @typeName3  varchar( 50)
16  if  exists ( select  *  from manager_product  where ID  =  @productID)
17  begin
18  select  @typeID  = productFirstType_ID, @typeID2  = productSecondType_ID, @typeID3  = productThirdType_ID 
19  from manager_product  where ID  =  @productID
20  print  ' typeIDs show :  ' + @typeID + ' , ' + @typeID2 + ' , ' + @typeID3
21  end
22 
23 
24  if( @typeID  is  null)
25  begin
26      select  @typeName  =  ''
27  end
28  else
29  begin
30     if  exists ( select  *  from manager_producttype  where ID  =  @typeID)
31       begin
32       select  @typeName  = typeName  from manager_producttype  where ID  =  @typeID
33       print  ' cunzai typeID :  '  +   @typeID  + ' ,typeName : ' +  @typeName
34       end
35 
36  end
37 
38 
39  if( @typeID2  is  null)
40  begin
41      select  @typeName2  =  ''
42  end
43  else
44  begin
45      if  exists ( select  *  from manager_producttype  where ID  =  @typeID2)
46      begin
47      select  @typeName2  = typeName  from manager_producttype  where ID  =  @typeID2
48      print  ' cunzai typeID2 :  '  +   @typeID2  + ' ,typeName2 : ' +  @typeName2
49      end
50  end
51 
52  if( @typeID3  is  null)
53  begin
54      select  @typeName3  =  ''
55  end
56  else
57  begin
58      if  exists ( select  *  from manager_producttype  where ID  =  @typeID3)
59      begin
60      select  @typeName3  = typeName  from manager_producttype  where ID  =  @typeID3
61      print  ' cunzai typeID3 :  '  +   @typeID3  + ' ,typeName3 : ' +  @typeName3
62      end
63  end
64  print  ' updateString :  ' +  @typeName  +  @typeName2  +  @typeName3
65  update manager_product  set  productKeyword  = productName  +  ' , '  +  @typeName  + ' , ' + @typeName2 + ' , ' + @typeName3
66  where ID  =  @productID

 好了,第一步经过测试,ok了。基本上完成了需求,可以实现:更新一条产品信息,将其关键字更新成需要的规则了。

    现在进行第二步,就是循环这个操作,对产品表的每一条数据进行更新。这里我选用了游标来完成。

 1  declare  @productID  nvarchar( 50)
 2  declare cursorOfID  cursor  for
 3    select ID  from manager_product
 4  -- 打开游标
 5  open cursorOfID
 6  -- 获取数据,游标下移一行
 7  fetch  next  from cursorOfID  into  @productID  
 8  -- 检测获取数据是否成功
 9  while  @@fetch_status = 0
10  begin
11    -- 显示通过游标赋值的变量
12     exec updateKeyword  @productID  -- 执行
13     -- 游标继续下移
14     fetch  next  from cursorOfID  into  @productID  
15  end
16  -- 关闭游标
17  close cursorOfID

 在第12行,调用了写好的存储过程来完成更新,每次更新完,让游标往下读一行,将新值赋给@productID,这样就实现了循环更新数据了。

测试,通过。

转载于:https://www.cnblogs.com/MoShin/archive/2011/10/04/2199167.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值