Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trigger to validate data

Status
Not open for further replies.

BHScripter

Technical User
Aug 26, 2002
159
US
Hi:

I am trying to create a trigger that forces our end users to choose a "location" before the entire record can be saved. The location is stored in column suser1. The primary key for each record is hmy. Here is my attempt, it worked once and then never again.


CREATE TRIGGER Work_Order_Location_Reqd
ON MM2WO
FOR Insert, Update
AS
IF EXISTS (Select 1 from
Inserted i
inner join deleted d on d.hmy = i.hmy
where i.suser1 = '' or i.suser1 = Null)

BEGIN
RAISERROR ('Location Cannot Be Blank', 16, 10)
Rollback Tran
END

Thanks in advance!
 
Firstly this isn't coded correctly. You should have "SELECT * FROM ...." rather than "SELECT 1 FROM ...". Yes your way will work but it is poor coding.

But the reason this isn't working is because you have i.suser1 = Null. This coding never returns what you expect as you cannot use = with a NULL value.

Your should code this as ..... i.suser1 IS NULL


Hope that helps.

Sam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top