Advertisement
jotaced

trigger asocia_ia_prest

Sep 13th, 2013
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.44 KB | None | 0 0
  1. ALTER TRIGGER [dbo].[asocia_ia_prest] ON [dbo].[GPF_DETALLE] FOR    INSERT
  2. AS
  3. BEGIN
  4.  
  5.     DECLARE @CORRELLOTE NUMERIC(9)
  6.     DECLARE @CORRELPW NUMERIC(9)
  7.     DECLARE @GPF_CORREL NUMERIC(5)
  8.    
  9.     CREATE TABLE #tabla(ID NUMERIC(8) IDENTITY(1,1), iad_codigo CHAR(2), prs_correl NUMERIC(5))
  10.    
  11.     SET @GPF_CORREL = (SELECT top 1 gpf_correl FROM INSERTED)
  12.  
  13.     IF EXISTS (SELECT iad_codigo FROM INSERTED WHERE iad_codigo IS NOT NULL /*and prs_correl is null*/)
  14.     BEGIN
  15.    
  16.         DECLARE @IAD_CODIGO CHAR(2)
  17.         DECLARE @PRS_CORREL NUMERIC(5)
  18.        
  19.         DECLARE @RowCount INT
  20.         DECLARE @I INT
  21.                
  22.         INSERT INTO #tabla(iad_codigo, prs_correl)
  23.         SELECT pria.IAD_CODIGO, pria.PRS_CORREL
  24.         FROM PRESTACION_IA pria (nolock)
  25.         WHERE pria.IAD_CODIGO IN (SELECT IAD_CODIGO FROM INSERTED WHERE prs_correl IS NULL)
  26.         ORDER BY pria.IAD_CODIGO, pria.PRS_CORREL
  27.        
  28.         SET @RowCount = (SELECT COUNT(ID) FROM #tabla)
  29.         SET @I = (SELECT top 1 id FROM #tabla)
  30.        
  31.         EXEC @CORRELLOTE = [dbo].[spu_get_correl_lote] 'GPF_DETALLE', @RowCount
  32.         SET  @CORRELPW =  @CORRELLOTE - @RowCount + 1
  33.         WHILE (/*@I <= @RowCount AND*/ @CORRELPW <= @CORRELLOTE)
  34.         BEGIN
  35.            
  36.             SELECT @IAD_CODIGO = iad_codigo, @PRS_CORREL = prs_correl FROM #tabla WHERE ID = @I
  37.                    
  38.             INSERT INTO GPF_DETALLE (GPD_CORREL, GPF_CORREL, IAD_CODIGO, PRO_CORREL, PRS_CORREL)
  39.             VALUES (@CORRELPW, @GPF_CORREL, @IAD_CODIGO, NULL, @PRS_CORREL)
  40.            
  41.             SET @CORRELPW = @CORRELPW+1
  42.             SET @I = @I  + 1
  43.         END
  44.        
  45.    
  46.     END
  47.    
  48. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement