Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

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.
0
Declan_Basile
Asked:
Declan_Basile
1 Solution
 
Ioannis ParaskevopoulosCommented:
Hi,

I would suggest calling a Stored Procedure for both functions instead of a triger. So you will probably have to change your app to use the sp instead of a standard Insert, but you may have more manageability this way.
I am not really font of triggers.

Let me know if you would like some sample code.

Giannis
0
 
Declan_BasileAuthor Commented:
Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now