create trigger ti_FE_ORDERS_POS on FE_ORDERS_POS 
INSTEAD OF INSERT 
as
DECLARE @NewId int;
DECLARE @RowNumber int;
DECLARE @CurrRow int;
DECLARE @CurYear int;
DECLARE @TableName varchar(128);
SET @NewId=0;
SET @CurrRow=1; 
select distinct year(table_year) t_y 
I nto #MyTmp 
 from inserted;
select * into #my_ins from inserted;
declare @CurYearVC varchar(4);
declare MyCurs cursor LOCAL 
    for select t_y from #MyTmp;
select @RowNumber=count(*) from #MyTmp;
open MyCurs;
WHILE @CurrRow<=@RowNumber 
  BEGIN
      FETCH NEXT FROM MyCurs into @CurYear; 
      set @CurYearVC=convert(varchar(4),@CurYear);
      set @TableName = ?FE_ORDERS_POS_?+ @CurYearVC;
      exec (?insert into ?+@TableName+? select * from
 #my_ins where year(table_year)=?+@CurYearVC);
      if @@Error<>0 begin return; end;
      SET @CurrRow=@CurrRow+1; 
  END;
deallocate MyCurs;
GO

Поделитесь материалом с коллегами и друзьями