Solved

SQL Server table restoration

Posted on 2013-11-18
3
158 Views
Last Modified: 2013-11-22
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
Comment
Question by:ScreenFox
3 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39656096
Probably yes, but it depends on what your trigger is doing. Can you post the code to the trigger?
0
 

Author Comment

by:ScreenFox
ID: 39656114
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
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39656263
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now