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
226 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

840 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