UnderStanding Cursors in SQL Server

Declare @v_userId int ,@v_emailId varchar(30),@v_usertypeId int,@v_name varchar(30)
DECLARE cur1 cursor FAST_FORWARD For select userId,emailId,userTypeId from tbl_UserMaster where userTypeId in (2,3)			
OPEN cur1  
FETCH NEXT FROM cur1 INTO @v_userId,@v_emailId,@v_usertypeId
WHILE (@@Fetch_status = 0)
BEGIN	  
	   IF @v_usertypeId = 2 -- Students
	   Begin
			select @v_name = firstName + lastName from tbl_StudentMaster where userId=@v_userId
			print 'Bidder Mail Id : '+@v_emailId+' <--> Name : '+@v_name
	   End						
	   Else -- Teachers
	   Begin
			select @v_name = teacherName from tbl_TeacherMaster where userId=@v_userId
			print 'Officer Mail Id'+@v_emailId+' <--> Name : '+@v_name
	   End
	   FETCH NEXT FROM cur1 INTO @v_userId,@v_emailId,@v_usertypeId 
End 
CLOSE cur1        
DEALLOCATE cur1						

Advertisements
This entry was posted in MS SQLServer 2008. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s