Solved

Writing Triggers to maintain data integrity

Posted on 2013-05-10
2
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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