2013-04-11 54 views
2

我有一個sql-server觸發器,它在開始時執行select操作。該選擇返回數百行,我想循環這些行。我怎樣才能循環返回的數據?通過sql server中的數據循環觸發器

這裏是我的觸發,我想循環@ProfId

Create TRIGGER EmailQueueRaw_Control_Trigger ON EmailQueueRaw FOR INSERT 
AS 
BEGIN 
    SELECT @ProfId= ProfessionalID,@Email= Email from Professionals 
END 

我怎麼能循環@ProfId?

回答

4

試試這個解決方案 -

ALTER TRIGGER [LabourAPI].[trg_IOIU_Employee] 

    ON [LabourAPI].[Employee] 
    INSTEAD OF INSERT, UPDATE 

AS BEGIN 

    SET NOCOUNT ON; 
    SET XACT_ABORT ON; 

    DECLARE 
      @EmployeeID INT 
     , @PersonID INT 
     , @TabelNumber VARCHAR(6) 
     , @DepartmentUID UNIQUEIDENTIFIER 
     , @PositionID INT 
     , @DateIn DATETIME 
     , @DateOut DATETIME 
     , @IsMainWorkPlace BIT 
     , @DatePension DATETIME 
     , @IsLabourUnionMember BIT 
     , @FiringReasonID INT 

    DECLARE cur CURSOR LOCAL READ_ONLY FAST_FORWARD FOR 
    SELECT 
      EmployeeID 
     , PersonID 
     , TabelNumber 
     , DepartmentUID 
     , PositionID 
     , DateIn 
     , DateOut 
     , IsMainWorkPlace 
     , DatePension 
     , IsLabourUnionMember 
     , FiringReasonID 
    FROM INSERTED 

    OPEN cur 

    FETCH NEXT FROM cur INTO 
      @EmployeeID 
     , @PersonID 
     , @TabelNumber 
     , @DepartmentUID 
     , @PositionID 
     , @DateIn 
     , @DateOut 
     , @IsMainWorkPlace 
     , @DatePension 
     , @IsLabourUnionMember 
     , @FiringReasonID 

    WHILE @@FETCH_STATUS = 0 BEGIN 

     SELECT 
       @PersonID 
      , @TabelNumber 
      , @DepartmentUID 
      , @PositionID 
      , @DateIn 
      , @DateOut 
      , @IsMainWorkPlace 
      , @DatePension 
      , @IsLabourUnionMember 
      , @FiringReasonID 

     FETCH NEXT FROM employee INTO 
       @EmployeeID 
      , @PersonID 
      , @TabelNumber 
      , @DepartmentUID 
      , @PositionID 
      , @DateIn 
      , @DateOut 
      , @IsMainWorkPlace 
      , @DatePension 
      , @IsLabourUnionMember 
      , @FiringReasonID 

    END 

    CLOSE cur 
    DEALLOCATE cur 

END