您现在的位置是:首页 >

数据库中游标是什么 实例讲解如何正确使用数据库游标

火烧 2022-09-04 15:36:10 1031
实例讲解如何正确使用数据库游标   在下面的示例中 表cla Name中有如下分类   具体示例  cla ID cla Name   衣服   裤子   帽子   鞋子  表 roductI fo有

实例讲解如何正确使用数据库游标  

  在下面的示例中 表className中有如下分类

  具体示例

  classID className

   衣服

   裤子

   帽子

   鞋子

数据库中游标是什么 实例讲解如何正确使用数据库游标

  表productInfo有如下记录

  productID productName parentID clickNum

   男士衣服 ——衣服类别中这条记录的点击率最高

   女士衣服

   男士裤子

   女士裤子

  ——裤子类别中这条记录点击率最高

   男士帽子

   女士帽子

  帽子类别中这条点击率最高

   男士鞋子

  ——鞋子类别中这条点击率最高

   女士鞋子

   女士鞋子

  现在我们要求分别把衣服 裤子 帽子 鞋子这些类别中点击率最高的一条记录找出来 然后再降序排列 结果如下

  productID productName clickNum

   男士衣服

   女士裤子

   男士鞋子

   女士帽子

  实现方法

  declare @temp table

  (

  productID int

  productName nvarchar( )

  clickNum int

  )

  declare @classID int

  declare cursor_classID cursor

  for

  select classID from dbo className

  open cursor_classID

  fetch next from cursor_classID into @classID

  —— 表示 FETCH 语句成功

  while @@FETCH_STATUS=

  begin

  insert into @temp

  select top productID productName clickNum from dbo productInfo

  where parentID = @classID

  order by clickNum desc

  fetch next from cursor_classID into @classID

  end

  close cursor_classID

  deallocate cursor_classID

lishixinzhi/Article/program/Oracle/201311/18574  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码