views:

48

answers:

3

Hi Guys,

What is the best way to set up a trigger to get all new rows in a table and insert them into a work queue table.

Many Thanks Sp

+1  A: 

With this trigger:

CREATE TRIGGER InsertIntoWorkingTable
    ON VeryOldHugeDatabaseTable
    INSTEAD OF INSERT 
AS
    insert into WorkingTable (column1, column2, column3...) (
        select column1, column2, column3
            from inserted
    )

All you have to do is to keep inserting into your VeryOldHugeDatabaseTable. The trigger will get busy inserting into this working table.

Once a while, you could simply launch a stored procedure through a job or something alike to insert or archive into this old table from the working table.

Will Marcouiller
I get the impression from his "work queue" comment that he's inserting in the main table and placing a FK in a separate table as some kind of action item attached to the row.
Kendrick
we have a database table that is v old and was originally built in access (its a nightmare) its currentlly 80 columns wide and has 1.5m rows. It's still being used to serve up the next customer using a complex stored proc, as you can imagine this is running extremlly slow. Im trying to transfer all new rows to another table (7 columns and will only ever have 100-200 rows) and once they are worked ill archive them, this way the front end users will see a major increase in speed..P.s the front end application is still and access database
Steven
I see the need now! =)
Will Marcouiller
@Steven. If you have some way of differentiating useful records from non useful records that is highly selective you should just be able to add it into the query (or create a view with this criteria rather than actually duplicating the rows). Given appropriate indexes and statistics the query optimiser should recognise this and perform the filtering early on.
Martin Smith
thanks for you help guysSp
Steven
A: 

A search for "t-sql trigger" returns this as the first result:

Create Trigger

Other than the syntax, you pretty much answered your own question. Set up a trigger AFTER INSERT and put an insert statement in it.

Kendrick
+2  A: 

To answer the question you asked

CREATE TRIGGER dbo.tr_i_BaseTable 
   ON  dbo.BaseTable 
   AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON;

   INSERT INTO WorkTable
   SELECT * FROM INSERTED

END

However instead of actually duplicating the rows twice can you not use some other attribute of the rows to detect "New" ones that should be considered part of the worktable. Such as ID or adding an "inserted" date/time field.

Or another approach might be to just start afresh with the new WorkTable and consider all previous records to be archived. You can create a (possibly Partitioned) View UNION ALL-ing the 2 tables for those queries that need to work on the consolidated set.

Martin Smith
I understand where your coming from with your comment.The DB table is a right mess and runs stupidly slow, thus i wanted to hold the "Work Queue" data seperate.Thanks for your code you have done it simillar to how i has envisaged.Thanks againSpMany thanks
Steven