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
230 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
ID: 20302086
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
ID: 20302790
every time a person completes the form
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20304058
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

679 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