Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Trigger to move records to history

Author  Topic 

Tovam
Starting Member

2 Posts

Posted - 2010-03-23 : 04:35:29
Hey

I have a table CISCOWORKS that contains dumps from Ciscoworks. Now I want to make a history table CISCOWORKS_HISTORY that contains all information I no longer need actively (entries that have the same CISCOWORKS_MAC as newer entries).

The two tables are like this, exactly the same.
DROP TABLE CISCOWORKS IF EXISTS;
CREATE TABLE CISCOWORKS
(
CISCOWORKS_ID NUMBER(9,0),
CISCOWORKS_MAC VARCHAR2(20 BYTE),
CISCOWORKS_SWITCH VARCHAR2(10 BYTE),
CISCOWORKS_PORT VARCHAR2(10 BYTE),
CISCOWORKS_VLAN VARCHAR2(10 BYTE),
CISCOWORKS_LASTSEEN TIMESTAMP (6),
CONSTRAINT CISCOWORKS_PK PRIMARY KEY (CISCOWORKS_ID)
);

DROP TABLE CISCOWORKS_HISTORY IF EXISTS;
CREATE TABLE CISCOWORKS_HISTORY
(
CISCOWORKS_HISTORY_ID NUMBER(9,0),
CISCOWORKS_HISTORY_MAC VARCHAR2(20 BYTE),
CISCOWORKS_HISTORY_SWITCH VARCHAR2(10 BYTE),
CISCOWORKS_HISTORY_PORT VARCHAR2(10 BYTE),
CISCOWORKS_HISTORY_VLAN VARCHAR2(10 BYTE),
CISCOWORKS_HISTORY_LASTSEEN TIMESTAMP (6),
CONSTRAINT CISCOWORKS_HISTORY_PK PRIMARY KEY (CISCOWORKS_HISTORY_ID)
);


I figured triggers will do the trick, but I'm not sure how they work.
I guess I will need a
DROP TRIGGER IF EXISTS CISCOWORKS_HISTORY_TRIGGER $$

CREATE TRIGGER CISCOWORKS_HISTORY_TRIGGER
ON CISCOWORKS
FOR INSERT
BEGIN
...
END;
$$


I guess I will have to check the INSERTED table and check every entry to see if there is a entry in the CISCOWORKS table with the same CISCOWORKS_MAC. If so, insert an entry in CISCOWORKS_HISTORY with the attributes of the CISCOWORKS entry, and then delete the CISCOWORKS entry.

Did I make any flaws in my reasoning? And how would I check every entry separately?
Thanks in advance.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-23 : 05:01:13
Is it possible that you are not working with MS SQL Server?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Tovam
Starting Member

2 Posts

Posted - 2010-03-23 : 05:11:11
That... might be possible.
It's Oracle I'm working with.
Sorry to have bothered.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-23 : 05:20:49
No problem - we would like to help you but I think you can get much better help from "Oracle-People"


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-23 : 10:07:05
try your luck at www.orafaq.com

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -