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

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?
AtourayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
why dont you change the insert statement like this

insert into detailsTable
select @Value1 , @Value2  
from MasterTable  WHERE LinkValue =  @Value2
0
AtourayAuthor Commented:
Oh let me make myself clear. An application is used for data entry.
0
Aneesh RetnakaranDatabase AdministratorCommented:
you can still use the above query
0
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

AtourayAuthor Commented:
Can you please elaborate on how to use this query?

Thanks.
0
Aneesh RetnakaranDatabase AdministratorCommented:
you dont have to create a trigger, just modify your exisitng INSERT statement like the one I posted above
0
AtourayAuthor Commented:
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
AtourayAuthor Commented:
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
AtourayAuthor Commented:
I've solved it myself....
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Security

From novice to tech pro — start learning today.