Solved

SQL 2005 insert trigger

Posted on 2010-11-29
8
224 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
which values u want to update of 'wafer' table.
 
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:iftech
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
very nice thank you
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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