Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. CREATE DATABASE test1
  2. GO
  3.  
  4. USE test1;
  5. GO
  6.  
  7. CREATE TABLE tableA (
  8. PK int PRIMARY KEY CLUSTERED
  9. ,someColumn varchar(50)
  10. )
  11. GO
  12.  
  13. CREATE DATABASE test2
  14. GO
  15.  
  16. USE test2;
  17. GO
  18.  
  19. CREATE TABLE tableB (
  20. PK int PRIMARY KEY CLUSTERED
  21. ,aForeignKey int NULL FOREIGN KEY REFERENCES test1.dbo.tableA(PK)
  22. )
  23. GO
  24.  
  25. Msg 1763, Level 16, State 0, Line 19
  26. Cross-database foreign key references are not supported. Foreign key 'test1.dbo.tableA'.
  27. Msg 1750, Level 16, State 0, Line 19
  28. Could not create constraint. See previous errors.
  29.  
  30. CREATE TRIGGER TR_Fake_FK ON dbo.tableB
  31. FOR INSERT, UPDATE
  32. AS
  33. BEGIN
  34. SET NOCOUNT ON;
  35.  
  36. IF EXISTS (
  37. SELECT *
  38. FROM INSERTED AS I
  39. WHERE NOT EXISTS (
  40. SELECT *
  41. FROM test1.dbo.tableA AS A
  42. WHERE I.aForeignKey = A.PK
  43. )
  44. )
  45. BEGIN
  46. RAISERROR('Violation of fake constraint',16,1);
  47. ROLLBACK;
  48. END
  49.  
  50. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement