Solved

SQL Server table restoration

Posted on 2013-11-18
3
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migrate a SQL 2008 to 2016, 2 45
SQL Improvement  ( Speed) 14 37
SQL trigger 5 34
Linked Server - SP with Param to VIew 7 24
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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