Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Server table restoration

Posted on 2013-11-18
3
Medium Priority
?
185 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 66

Accepted Solution

by:
Jim Horn earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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 …
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

618 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