?
Solved

Check if value exists in master table before insert into details table.

Posted on 2009-07-09
8
Medium Priority
?
492 Views
Last Modified: 2012-05-07
I have a details table and master table. The master table is linked to the details table by a unique key in the master table. The link field cannot be primary keys on both tables otherwise I would have use foreign keys. Now I want a trigger that will be fired on insert of the details table. The trigger should check if the link value exists in the master table and allow the insertion. If it doesn't exist the insertion should fail.
 Any ideas how to do this?
0
Comment
Question by:Atouray
[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
  • 5
  • 3
8 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24814549
why dont you change the insert statement like this

insert into detailsTable
select @Value1 , @Value2  
from MasterTable  WHERE LinkValue =  @Value2
0
 

Author Comment

by:Atouray
ID: 24814663
Oh let me make myself clear. An application is used for data entry.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24815152
you can still use the above query
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:Atouray
ID: 24816435
Can you please elaborate on how to use this query?

Thanks.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24817129
you dont have to create a trigger, just modify your exisitng INSERT statement like the one I posted above
0
 

Author Comment

by:Atouray
ID: 24821427
I don't have an existing insert statement. As I told you the application being user for data entry connects directly to the details.
0
 

Author Comment

by:Atouray
ID: 24821673
I've found a way to rollback the transaction but I need to print the message to the user. I've tried using raiserror but it doesn't show anything on the application however when I use test on the sql server it works. How do I print the error to the application connected via odbc to the database.
0
 

Accepted Solution

by:
Atouray earned 0 total points
ID: 24825849
I've solved it myself....
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Make the most of your online learning experience.
A look at what happened in the Verizon cloud breach.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

771 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