Solved

Writing Triggers to maintain data integrity

Posted on 2013-05-10
2
191 Views
Last Modified: 2013-05-13
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
Comment
Question by:Declan_Basile
2 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39157655
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
 
LVL 1

Author Closing Comment

by:Declan_Basile
ID: 39162589
Thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now