2015-05-18 15 views
5

Avere un trigger di inserimento post. la tabella originale in cui viene scritto ha un campo quantità che prenderà qualsiasi numero. Tuttavia, il trigger che viene generato dopo l'inserimento deve scrivere una tabella di transazione una volta per ciascun QTY. Quindi se il QTY originale è 4 allora quando il trigger scatta deve scrivere il record quattro volte.Scrittura dopo trigger di inserimento nel server SQL

USE [BLAH] 
GO 

SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 


ALTER TRIGGER [dbo].[Track_Change_Detail] 
    ON [dbo].[MYtABLE] 
    AFTER Insert 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    Declare @Reason varchar(255), 
    @TransID nvarchar(10), 
    @Qty bigint, 
    @UserID nvarchar(10), 
    @Disp nvarchar(3), 
    @ItemNumber nvarchar(20), 
    @ItemLevel nchar(1), 
    @LocalQTY Int 

    Declare @curChg Cursor 
    begin 
     --insert into BLAH.dbo.Transactions 
     set @curChg = CURSOR FAST_FORWARD FOR 
     SELECT    inserted.TransiD, 
          inserted.Item_num, 
          inserted.Quantity, 
          inserted.Logged_in, 
          Inserted.Lvl, 
          Inserted.Disposition 

     FROM    inserted 
     Where    Inserted.disposition = 'RTS' 


     OPEN @curChg 
     if (@@error != 0) goto EndError 

     fetch next from @curChg into @Transid,@ItemNumber,@QTY,@UserID,@Itemlevel,@Disp 

     if (@@error != 0) goto EndError 

     while @@FETCH_STATUS = 0 
     begin 
     Set @LocalQTY = 0 
     --if @Disp = 'RTS' 
      while @localQTy <= @Qty 

      insert BLAH.dbo.Transactions ( 
               [Status], 
               Area, 
               Location, 
               Item, 
               [Level], 
               Quantity, 
               TransTime, 
               [Source], 
               Lot, 
               [ExpireDate], 
               RecvDate, 
               UserID, 
               [Weight], 
               Temperature, 
               Reference, 
               CoolCode, 
               Serial, 
               ToArea, 
               ToLocation) 
       values       (
               'New', 
               NULL, 
               NULL, 
               @ItemNumber, 
               @ItemLevel, 
               1, 
               Getdate(), 
               'A', 
               @LOT, 
               NULL, 
               getdate(), 
               @UserID, 
               NULL, 
               NULL, 
               @TransID, 
               Null, 
               Null, 
               'RTN', 
                '1') 

      Set @LocalQTY =+1 
      if @localQTY = @QTY goto enderror 
      fetch next from @curChg into @Transid,@ItemNumber,@QTY,@UserID,@Itemlevel,@Disp 
      if (@@error != 0) goto EndError 
     end 

     close @curChg 
     deallocate @curChg 

    end 

EndError: 

END 

se non mi importa di scrivere 1 record per ogni funziona scrivendo 1 record con QTY 3. sto indovinando la variabile @localqty è il mio problema. Sono chiudere o qualcuno può indirizzare nella giusta direzione

Grazie

+1

Puoi farlo facilmente senza cursots, che sarà molto più efficiente –

+1

Cerca una risposta che ti dirà come farlo senza usare un cursore. Tuttavia, nel codice, è necessario bloccare le istruzioni di codice nel ciclo while con BEGIN ed END (altrimenti solo l'insert viene eseguito nel ciclo e l'iteratore di loop non viene mai incrementato). Inoltre, non ho mai visto la sintassi come SET @ LocalQTY = + 1, non penso che incrementi il ​​valore come credi, penso che sia impostato su 1. Usa SET @ LocalQTY = @ LocalQTY + 1. Ma non indossare lo farò comunque! ;) –

risposta

2

Ecco un esempio di come si può fare questo senza un cursore:

create table log(id int, qty int) 
insert into log values 
(1, 5), 
(2, 3), 
(3, 10) 


select * from log 
cross apply 
(select top(log.qty) 1 as d from 
(values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) t1(n) cross join 
(values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) t2(n) cross join 
(values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) t3(n))ca 

uscita:

id qty 
1 5 
1 5 
1 5 
1 5 
1 5 
2 3 
2 3 
2 3 
3 10 
3 10 
3 10 
3 10 
3 10 
3 10 
3 10 
3 10 
3 10 
3 10 
0

Sostituisci questo brutto cursore con questa semplice query di selezione ........

insert BLAH.dbo.Transactions ([Status], Area, Location, Item, [level], Quantity, TransTime, 
          [Source],Lot,[ExpireDate],RecvDate,UserID,[Weight],Temperature, 
          Reference,CoolCode,Serial,ToArea,ToLocation) 
SELECT 'New', 
     NULL, 
     NULL, 
     i.Item_num, 
     i.Lvl, 
     1, 
     Getdate(), 
     'A', 
     -- @LOT, alien variable dont know where this came from 
     NULL, 
     getdate(), 
     i.Logged_in, 
     NULL, 
     NULL, 
     i.TransiD, 
     Null, 
     Null, 
     'RTN', 
      '1' 
FROM Inserted i CROSS APPLY (SELECT TOP (i.Quantity) t.number 
           FROM master..spt_values t 
           CROSS JOIN master..spt_values t2)t(number) 
Where i.disposition = 'RTS' 
Problemi correlati