Solved

SQL Server table restoration

Posted on 2013-11-18
3
156 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 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

21 Experts available now in Live!

Get 1:1 Help Now