Solved

before update trigger to prevent a field edit

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

687 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