Link to home
Start Free TrialLog in
Avatar of iftech
iftech

asked on

SQL 2005 insert trigger

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.
Avatar of joevi
joevi
Flag of United States of America image

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?
Avatar of Sara bhai
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
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.
which values u want to update of 'wafer' table.
 
Avatar of iftech
iftech

ASKER

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.    
ASKER CERTIFIED SOLUTION
Avatar of joevi
joevi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

Avatar of iftech

ASKER

very nice thank you