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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

914 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

13 Experts available now in Live!

Get 1:1 Help Now