Use trigger to validate against a view

I would like to find out if the a part exists when it is entered.  I have a table called Snum and a View called validpart. When an insert is done on the PartNumber field in the Snum table I want it to see if that part exists in the validpart view.  If it does great- if not then error rollback etc...  It's my understand that you cannot create relationship with a view so my only options are either a trigger or constraint.
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.

you can do it in a trigger
but you'd be probably be better off if you did the validation more up front on wether the part was valid...

create trg_ins_snum on table snum
for Insert


if not exists (select partno from validpart as v inner join inserted as i on v.partno = i.partno)
    rollback transaction
dloebigAuthor Commented:
Any idea why it keeps yelling at me about the syntax around "Error"?

CREATE TRIGGER [ValidPart] ON [dbo].[SerialNumber]

if not exists (select PartNumber from ValidProduct as v inner join inserted as i on v.PartNumber = i.PartNumber)
    rollback transaction
    raiserror (Error)


yes because you need to properly configure the raiserror arguments...

RAISERROR ( { msg_id | msg_str | @local_variable }
    { ,severity ,state }
    [ ,argument [ ,...n ] ] )
    [ WITH option [ ,...n ] ]


RAISERROR (N'This is message %s %d.', -- Message text.
           10, -- Severity,
           1, -- State,
           N'number', -- First argument.
           5); -- Second argument.


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
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.