Tuesday, 11 October 2011

CURSOR EXAMPLE SQL SERVER


Create PROC [dbo].[InsertInto_ContactBGCMAddress]     
   @BCE_Address_ID varchar(100)     
     ,@Address_Line_1 varchar(100)     
     ,@City varchar(50)     
     ,@State_Province varchar(100)     
     ,@Postal_Area varchar(100)     
     ,@Country varchar(100)     
     ,@Phone_Number varchar(100)  
AS
BEGIN   
 Begin tran insertContact
  DECLARE @BGCM_ID INT, @BiometContactId VARCHAR(100)  
  Declare curContact Cursor
  For 
  select From_BGCM_ID from PMP_BGCMAffiliation where To_BGCM_ID =  @BCE_Address_ID               
  open  curContact
  Fetch next from curContact into @BiometContactId
  While @@FETCH_STATUS = 0
  Begin
 
   SELECT @BGCM_ID = BGCM_ID FROM PMP_BGCMCustomer WHERE BCE_ID= @BiometContactId  and Customer_Type = 'Physician'
   INSERT INTO PMP_BGCMAddress     
     ([BGCM_ID]     
     ,[BCE_Address_ID]     
     ,[Last_Modified_Date]     
     ,[Last_Modified_Source]     
     ,[Address_Line_1]     
     ,[City]     
     ,[State_Province]     
     ,[Postal_Area]     
     ,[Country]     
     ,[Phone_Number]     
     ,[Create_Date]     
     ,[Address_Status_Code])     
   VALUES     
     (@BGCM_ID     
     ,@BCE_Address_ID     
     ,getdate()     
     ,'CE'     
     ,@Address_Line_1     
     ,@City     
     ,@State_Province     
     ,@Postal_Area     
     ,@Country     
     ,@Phone_Number     
     ,getdate()     
     ,'Active')
    

    --Update PMP_BGCMAffiliation SET From_BGCM_ID = @BGCM_ID where From_BGCM_ID = @BiometContactId
      
   Fetch next from curContact into @BiometContactId
  End
  close curContact
  DEALLOCATE curContact
 Commit tran insertContact
End

No comments:

Post a Comment