Thursday, 22 June 2017

Looping With Cursor SQL Server

Cursor is an database object which is use when there is a need to process table record row by row . you can access each field of your table at record by record.

USE [Girfa_tportal]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter procedure [dbo].[GetStudentContact]
as
begin
Declare @phone as varchar(15)
Declare @email as varchar(50)


Declare MY_data CURSOR FOR
select Phone,email from [dbo].[StudentMaster]
open MY_data
FETCH NEXT FROM MY_data INTO @phone ,@email
        WHILE @@FETCH_STATUS = 0
        BEGIN

           select @phone as contact,@email as Email

        FETCH NEXT FROM MY_data INTO @phone ,@email
        END
    CLOSE MY_data
DEALLOCATE MY_data

end