Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

688 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