Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Trigger Raise Error

Author  Topic 

sinva
Starting Member

23 Posts

Posted - 2010-04-08 : 10:18:10
I have never written a trigger before and now am seeing the light. Is
there a way to write a trigger so that if a user changes on a column A in
a single row on table A then the trigger could check if other table B has a column X equals to table A column B and table B column Y has a value equals to "A" will raise an error. Thanks,

Create table A
(
A varchar(10),
B varchar(10)
)


Create table B
(
X varchar(10),
Y varchar(10)
)

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2010-04-08 : 10:28:19
Yes, you can write a trigger, but please keep in mind that trigger is not fired for single row but for entire scope of statement. For example, if your update statement updates 10 rows, trigger will be fired only once for all the 10 rows.

Harsh Athalye
http://www.letsgeek.net/
Go to Top of Page

sinva
Starting Member

23 Posts

Posted - 2010-04-08 : 10:30:57
Would you mind please post some exmaple here? As I do not have any idea about it now.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-08 : 11:01:56
something like

CREATE TRIGGER Yourtrigger
ON YourTable
AFTER INSERT,UPDATE
AS
BEGIN
IF EXISTS (SELECT 1 FROM INSERTED i
JOIN TableB b
ON b.X=i.B
AND b.Y = i.A)
RAISERROR ('Your Error Message', 16,1 )
END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -