• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

SQL Server table restoration

Hello,

I have a table with a trigger that whenever a row is inserter to the table the trigger does some calculations an stores them in another table.

Is there a way to create a procedure that does this with all the rows that are already inserted in the table?
0
ScreenFox
Asked:
ScreenFox
1 Solution
 
Lee SavidgeCommented:
Probably yes, but it depends on what your trigger is doing. Can you post the code to the trigger?
0
 
ScreenFoxAuthor Commented:
This is the code of the trigger:

BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	

    @QTY int,@QTY1 int, @POS_ACTN int, @CUE varchar(10),@VTO int, @ID int,@SYM varchar(10),
    @AVP float, @MED float, @MED_NUEVO float,@POS_ACT int, @RLZD float, @RLZD_1Y float,
    @MULTIPLICADOR smallint, @RLZD_ORIGINAL float,@ULTIMO_P float,@URLZD float,
	@ANY smallint,@MES smallint,@DIA smallint,@HOR smallint,
	@MIN smallint,@SEG smallint,@URLZD_ORIGINAL float,@FACTOR float,@MES_A smallint,
	@ANYO_A smallint,@SYM_ANT varchar(10),@VTO_ANT int,@CUENTA_ANT varchar(50),@ID_E_ANT smallint,
	@ID_TIT int,@ID_BRO int,@CURR varchar(10),@COMISION float,@rlzdpto float,@OID varchar(100),@CARA varchar(1)
	,@EXCH varchar(10),@CALCCOM float,@OQT int;
    

    @AVP = AVP, @ANY = ANYO,@MES = MES,@DIA = DIA,@HOR = HORA,@MIN = MINUTO,@SEG = SEGUNDO,
    @CURR = CURR,@OID = OID,@OQT = OQT FROM INSERTED
       
    IF(@OID LIKE '%-A-%')
    BEGIN
		SET @CARA = 'A'
    END
    
    IF(@OID LIKE '%-B-%')
    BEGIN
		SET @CARA = 'B'
    END
    
    IF(@OID NOT LIKE '%-B-%' AND @OID NOT LIKE '%-A-%' )
    BEGIN
		SET @CARA = '*'
    END 
      
    SELECT @ID_BRO = ID_BROKER FROM F_CUENTA_BROKER WHERE CUENTA = @CUE
    SELECT @ID_TIT = ID_TITULO FROM F_TITULO_BROKER_SYM WHERE SYM = @SYM AND VTO = @VTO
    SELECT @EXCH = EXCHANGE FROM L_TITULOS WHERE ID_TITULO = @ID_TIT 
    SELECT @FACTOR = FACTOR FROM F_BROKER_SYM_FACTOR WHERE SYM = @SYM AND ID_BROKER = @ID_BRO
    SELECT @COMISION = COMISION FROM F_SYM_CURR_COMISION WHERE SYM = @SYM AND CURR = @CURR
	IF(@COMISION IS NULL)
		BEGIN
		SET @COMISION = 0;
		END
    SET @ULTIMO_P = @AVP
    SELECT @MULTIPLICADOR = MULTIPLICADOR FROM dbo.L_TITULOS WHERE ID_TITULO = @ID_TIT
	SET @ULTIMO_P = @ULTIMO_P * @FACTOR	

    

    
    INSERT INTO dbo.F_POSICION_ACTUAL (POSICION,ID_ESTRATEGIA,SYM,VTO,CUENTA,
    HORA,MINUTO,SEGUNDO,ANYO,DIA,MES,RLZD_1Y,PRECIO_MEDIO,CURR,CARA) SELECT 0,ID_ESTRATEGIA,SYM, 
    VTO,CUENTA,HORA,MINUTO,SEGUNDO,ANYO,DIA,MES,0,0,@CURR,@CARA FROM inserted WHERE NOT EXISTS (SELECT * 
    FROM dbo.F_POSICION_ACTUAL WHERE VTO = @VTO AND ID_ESTRATEGIA = @ID 
    AND @SYM = SYM AND CUENTA = @CUE AND @CARA = CARA)
    
    SELECT @POS_ACT = POSICION, @MED = PRECIO_MEDIO, @RLZD_ORIGINAL = RLZD_1Y,
    @URLZD_ORIGINAL = URLZD
    FROM dbo.F_POSICION_ACTUAL WHERE VTO = @VTO AND ID_ESTRATEGIA = @ID AND @SYM = SYM AND CUENTA = @CUE 
    AND @CARA = CARA


    IF(@EXCH = 'IDEALPRO')
		BEGIN
	    SET @AVP = @AVP + (@COMISION*SIGN(@QTY)*@AVP) 
		END
    
    ELSE
		BEGIN
	    SET @AVP = @AVP + @COMISION*SIGN(@QTY)
		END
	
    IF (@POS_ACT = 0) 
		SET @MED_NUEVO = @AVP
    IF (@POS_ACT > 0 AND @QTY > 0) OR (@POS_ACT < 0 AND @QTY < 0)
		SET @MED_NUEVO = ((abs(@POS_ACT)*@MED)+(abs(@QTY)*@AVP))/(abs(@POS_ACT)+abs(@QTY))
	ELSE
		BEGIN
		IF (@POS_ACT > 0 AND @QTY < 0)  or (@POS_ACT < 0 AND @QTY > 0)
			BEGIN
			
				IF (ABS(@QTY) > ABS(@POS_ACT))
					BEGIN
					SET @QTY1 = (-1*(@POS_ACT))
					SET @RLZD = ((-1*(@AVP -@MED)) * (@QTY1) *(@MULTIPLICADOR))
					SET @rlzdpto = @RLZD/ABS(@QTY1)
					SET @MED_NUEVO = @AVP
					END
				ELSE
					begin
					SET @RLZD = ((-1*(@AVP -@MED)) * (@QTY) *(@MULTIPLICADOR))
					SET @rlzdpto = @RLZD/ABS(@QTY)
					end
			END
		END


    SELECT @POS_ACTN = SUM(@QTY + POSICION), @RLZD_1Y = SUM(@RLZD_1Y+RLZD_1Y)
    FROM dbo.F_POSICION_ACTUAL WHERE VTO = @VTO AND ID_ESTRATEGIA = @ID AND 
    @SYM = SYM AND CUENTA = @CUE
	
	SET @POS_ACTN = @QTY + @POS_ACT 
    SET @RLZD_1Y = @RLZD+@RLZD_ORIGINAL
	SET @URLZD = ((@ULTIMO_P-@MED_NUEVO)*(@POS_ACTN*@MULTIPLICADOR))


	
    UPDATE dbo.F_POSICION_ACTUAL SET POSICION = @POS_ACTN, PRECIO_MEDIO =
	ISNULL(@MED_NUEVO,@MED),RLZD_1Y = ISNULL(@RLZD_1Y,@RLZD_ORIGINAL),ANYO = @ANY,MES = @MES,
	DIA = @DIA,HORA=@HOR,MINUTO=@MIN,SEGUNDO=@SEG,URLZD = ISNULL(@URLZD,0)
	WHERE VTO = @VTO AND ID_ESTRATEGIA = @ID AND @SYM = SYM AND CUENTA = @CUE AND CARA = @CARA
   
    INSERT INTO dbo.F_HISTORICO_POSICION (POSICION,ID_ESTRATEGIA,SYM,VTO,CUENTA,
    HORA,MINUTO,SEGUNDO,ANYO,DIA,MES,RLZD_OP, RLZD_1Y,PRECIO_MEDIO,URLZD,CURR,RLZD_PTO,CARA,OQT,QTY) SELECT @POS_ACTN,ID_ESTRATEGIA,
    SYM, VTO,CUENTA,HORA,MINUTO,SEGUNDO,ANYO,DIA,MES,ISNULL(@RLZD,0), ISNULL(@RLZD_1Y,@RLZD_ORIGINAL),
    ISNULL(@MED_NUEVO,@MED),@URLZD,@CURR,@rlzdpto,@CARA,@OQT,@QTY FROM inserted 
   
  
END

Open in new window

0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
I don't see anything in here that will prevent you from creating a SP and performing this action for all rows.  You may have to add more code though, to prevent INSERTs from occuring for rows that have already been INSERTed, to avoid any duplicate rows issues.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now