Solved

before update trigger to prevent a field edit

Posted on 2006-11-23
3
2,187 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
[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
  • 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

733 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