About Me

Barking, Essex, United Kingdom
MCITP DBA ,MCITP BI & Oracle OCP 11G

Saturday, April 6, 2013

Introduction to Cursor

  use test --database name
  declare @AuctionID int
  declare @Bidamount float
  declare @BidDate smalldatetime
  Declare @userid int

  declare cur_fetch cursor for
  ---declare the cursor
  select * from [bidauction]

  open  cur_fetch
  ---open the cursor


  fetch next from cur_fetch into @AuctionID,@Bidamount,@BidDate,@userid
  ---fetch the row

  while(@@FETCH_STATUS=0)
  Begin
  --concatenate operation plus use cast in order to match char [string]values
  print  '[AuctionID] ='+cast(@AuctionID as varchar(5))
  +'[BidAmount] ='+cast(@Bidamount as varchar(10))+'BidDate ='+
  cast(@BidDate as varchar)+space(4)+'UserID='+cast(@userID as varchar)
     
  fetch next from cur_fetch into @AuctionID,@Bidamount,@BidDate,@userid
  --process the fectched row and manipulate operation row by row basis
  
  end

  close cur_fetch --close the cursor

  deallocate cur_fetch---deallocate the memeory from cursor

No comments:

Post a Comment