Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE DATABASE test1
- GO
- USE test1;
- GO
- CREATE TABLE tableA (
- PK int PRIMARY KEY CLUSTERED
- ,someColumn varchar(50)
- )
- GO
- CREATE DATABASE test2
- GO
- USE test2;
- GO
- CREATE TABLE tableB (
- PK int PRIMARY KEY CLUSTERED
- ,aForeignKey int NULL FOREIGN KEY REFERENCES test1.dbo.tableA(PK)
- )
- GO
- Msg 1763, Level 16, State 0, Line 19
- Cross-database foreign key references are not supported. Foreign key 'test1.dbo.tableA'.
- Msg 1750, Level 16, State 0, Line 19
- Could not create constraint. See previous errors.
- CREATE TRIGGER TR_Fake_FK ON dbo.tableB
- FOR INSERT, UPDATE
- AS
- BEGIN
- SET NOCOUNT ON;
- IF EXISTS (
- SELECT *
- FROM INSERTED AS I
- WHERE NOT EXISTS (
- SELECT *
- FROM test1.dbo.tableA AS A
- WHERE I.aForeignKey = A.PK
- )
- )
- BEGIN
- RAISERROR('Violation of fake constraint',16,1);
- ROLLBACK;
- END
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement