Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Constraints across tables

Author  Topic 

Christopher Fox
Starting Member

1 Post

Posted - 2015-02-01 : 13:55:41
I need to prevent a user from entering a serial number that already exists in any of four other tables. Can a constraint check other tables? Can a constraint contain some SELECT statements? Thanks for any help!

robvolk
Most Valuable Yak

15732 Posts

Posted - 2015-02-01 : 16:42:54
The only inter-table constraint supported by SQL Server is a foreign key. You would have to code a multi-table duplicate check in a stored procedure or trigger.

Any reason why the serial numbers are in 4 different tables and not one table?
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-02-01 : 22:26:03
You can do what you want. Write a UDF that takes the number to check, checks the other 4 tables and returns a bit. Call the UDF in the constraint and check the
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-02-01 : 22:26:35
Result
Go to Top of Page
   

- Advertisement -