Solved

How would I create a trigger that will copy the content of one record in a Table to a different table

Posted on 2007-11-16
3
196 Views
Last Modified: 2008-08-12
I want the trigger to fire only if the field named complete is = true.

But I only want it to copy it once not every time someone completes the form.  This is because the data in the 2ed, table my get edited, and I don’t want the info over written every time another person marks a form complete.(true)

Is there a sample of how I would do this?

I’m using SQL server 2005.

Thanks for any help!
0
Comment
Question by:bmanmike39
3 Comments
 
LVL 7

Expert Comment

by:assyst
Comment Utility
Are you updating the complete form only once or do you do the update everytime if someone completes the same form.
If the complete field is updated only once then you can write a trigger like
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[trg_ins_Table2]
   ON  [dbo].[Table1]
   AFTER UPDATE
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.

          -- Insert statements for trigger here
            
            if update(LastLogin)

          BEGIN
                                                                              INSERT INTO [Table2]
               ([ID]
               ,[Field2]
               ,[Field3]
            SELECT [Table1]
              ,[ID]
              ,[Field1]
              ,[Field2]
              , [Field3]                                                  
              FROM inserted
                                                            
          END

END
0
 

Author Comment

by:bmanmike39
Comment Utility
every time a person completes the form
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
Comment Utility
assuming there is a link between table2 and table1
CREATE TRIGGER dbo.triggername

ON dbo.tablename

AFTER UPDATE, INSERT

AS

BEGIN

  INSERT INTO table2

  SELECT F1, F2, F3 FROM INSERTED I

    LEFT JOIN table2 T on T.ID = I.ID

  WHERE I.COMPLETED = 1   -- fire only if the field named complete is = true.

    AND T.ID IS NULL      -- don't want the info over written 

END

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

772 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

11 Experts available now in Live!

Get 1:1 Help Now