Avatar of Declan Basile
Declan Basile
Flag for United States of America asked on

Writing Triggers to maintain data integrity

I have an Access 2003 application with SQL Server 2012 linked tables.

The "Locations" table has four fields of concern, namely ParentId, WIP, Reserved, OwnedByParent

WIP, Reserved, and OwnedByParent are smallint datatype, can only be either -1 or 0 per a check constraint, and can not be Null.  ParentId can be Null and is an int datatype.

The Requirement: Whenever there is a record with a value for ParentId (i.e. ParentId is not Null) and WIP=0, there needs to also be one and only one record with WIP = -1 and the same values for ParentId, Reserved, and OwnedByParent as that record.

I think this can be accomplished by:
1.) Creating a trigger such that whenever a record with a value for ParentId is inserted, a record is also inserted with WIP = -1 and the same ParentId, Reserved, and OwnedByParent values as the record being inserted if a record meeting that criteria doesn't already exist, and
2.) Create another trigger that rolls back a transaction if a record being inserted has WIP = -1 and there already exists a record with WIP = -1 and with the same ParentId, Reserved, and OwnedByParent values as the record being inserted.

Is there an easier way to accomplish the requirement?  If not how can I write the triggers to do this?  I prefer not to put this code in the front end application because I'd have to include code in any part of the application that is used to maintain data in the locations table, however that is an option if it is too complicated to do with triggers in SQL Server.
Microsoft SQL ServerMicrosoft Access

Avatar of undefined
Last Comment
Declan Basile

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ioannis Paraskevopoulos

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Declan Basile

ASKER
Thanks
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes