Solved

SQL 2005 insert trigger

Posted on 2010-11-29
8
225 Views
Last Modified: 2012-06-21
Hi All,

I have a 2 simple tables
CREATE TABLE [dbo].[OCRRawData] (
      [OCRRawDataID] [int] IDENTITY (1, 1) NOT NULL ,
      [WaferNumber] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [FrontSideLasermark] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [OCRDate] [datetime] NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Wafer] (
      [OCRRawDataID] [int] IDENTITY (1, 1) NOT NULL ,
      [WaferNumber] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [FrontSideLasermark] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [OCRDate] [datetime] NULL
) ON [PRIMARY]
GO

I looking to create an insert trigger on OCRRawData table. When a record is inserted I want to update the Wafer table with the values from OCRRawData. The records will already exist in the Wafer table so it must be an update and several records can be inserted in OCRRawData at once.
the Wafer table will have other fields in it but I did not include them here because they are not needed now.
0
Comment
Question by:iftech
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 4

Expert Comment

by:joevi
ID: 34234565
A few questions on your design before moving on to a trigger:
1) What are the functions of the two tables? Is OCRRawData on the Many side of the relationship?
2) Is the PK/FK relationship between them the 4 columns shown?
3) If there is a relationship between them why is OCRRawDataID an identity column in both?
4) How are you handling referential integrity?
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 34236397
Create trigger tr_update on OCRRawData after insert as update Wafer set wafernumber = inserted.wafernumber from wafer  inner join inserted  on wafer.ocrrawdataid= inserted.ocrrawdataid
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 34238738
Hi,

Wanna insert data of one table to another using TRigger then you can get the inserted row in "Inserted" Table and insert requiredcolumns to another table.
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 34238900
which values u want to update of 'wafer' table.
 
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:iftech
ID: 34242474
Thanks for the response,

1) The WaferNumber in the Waffer table will be unique. The WaferNumber in the OCRRawData may be duplicated but I can delete inserted records after the trigger runs to ensure no duplicates in the table if needed.
2)No, there is  no relationship with PK/FK between the tables they will join on the WaferNumber
3)Wafer table is a sample table the actual identity of table will be different

I want to insert the whole row only if the WafferNumber in OCRRawData does not exist in the Wafer table.  if it is there I want to update the fields in the Wafer table from the fields in the OCRRawData table.    
0
 
LVL 4

Accepted Solution

by:
joevi earned 500 total points
ID: 34242852
I left Inserts/Updates for OCRRawDataID since it's an identity column in both tables but the trigger below can be modified to accommodate any changes.

Create TRIGGER trOCRRawData
   ON OCRRawData
   AFTER INSERT
AS
BEGIN
      SET NOCOUNT ON;
      --Insert New Values
      INSERT INTO Wafer (WaferNumber, FrontSideLasermark, OCRDate)
      SELECT     Inserted.WaferNumber, Inserted.FrontSideLasermark, Inserted.OCRDate
      FROM         Inserted LEFT OUTER JOIN
                      Wafer ON Inserted.WaferNumber = Wafer.WaferNumber
      WHERE     (Wafer.WaferNumber IS NULL)
      --Update Existing Values
      UPDATE    Wafer
      SET  FrontSideLasermark = Inserted.FrontSideLaserMark, OCRDate = Inserted.OCRDate
      FROM         Inserted INNER JOIN
    Wafer ON Inserted.WaferNumber = Wafer.WaferNumber
END
0
 
LVL 4

Expert Comment

by:joevi
ID: 34243027
Correction:
I left OUT Inserts/Updates for OCRRawDataID since it's an identity column in both tables ....
and
you'll obviously want to add error trapping to suite your needs.

0
 

Author Closing Comment

by:iftech
ID: 34249188
very nice thank you
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL - sum months, quarter, YTD 9 91
Record open by another user 6 50
Get row count of current SQL query 8 44
configure service broker on all databases 2 76
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

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

20 Experts available now in Live!

Get 1:1 Help Now