Solved

before update trigger to prevent a field edit

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

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.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

773 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