Solved

before update trigger to prevent a field edit

Posted on 2006-11-23
3
2,186 Views
Last Modified: 2008-01-09
Can someone show me a sample of a trigger that at the time of an insert copies the value in field1 to field2 and also on an edit prevents field2 from being altered?

Lets assume both fields are INT


Thanks!
0
Comment
Question by:schworak
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 18005759
create TRIGGER MyTrig
ON mytable
FOR  INSERT
AS
BEGIN
UPDATE mytable
   SET field2 = inserted.field1
      FROM inserted, mytable
         WHERE inserted.keyvalue = mytable.keyvalue
             
END
0
 
LVL 29

Assisted Solution

by:Gautham Janardhan
Gautham Janardhan earned 500 total points
ID: 18005760
create TRIGGER MyTrigUPD
ON mytable
FOR  UPDATE
AS
BEGIN
UPDATE mytable
   SET field2 = deleted.field1
      FROM deleted, mytable
         WHERE deleted.keyvalue = mytable.keyvalue
             
END
0
 
LVL 3

Author Comment

by:schworak
ID: 18008181
Thanks!

I have been working in Oracle for a while now and been neglecting my SQL Server skills (which weren't all that strong to start with) so I was having trouble getting started.

This gave me what I needed now on to making it the full complexity I needed.

Great answers!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

792 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