sp_CreateForeignKey.txt 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. CREATE PROCEDURE sp_CreateForeignKey
  2. @PrimaryTable SYSNAME = NULL,
  3. @PrimaryField SYSNAME = NULL,
  4. @ForeignTable SYSNAME = NULL,
  5. @ForeignField SYSNAME = NULL
  6. AS
  7. BEGIN
  8. -- =============================================
  9. -- Create date: 12/28/2008
  10. -- Description: Creates a primary key foreign key relationship.
  11. -- =============================================
  12. DECLARE @Instructions TABLE
  13. (
  14. [INFO] NVARCHAR(4000)
  15. )
  16. DECLARE @sSQL NVARCHAR(4000)
  17. DECLARE @sConstraint NVARCHAR(4000)
  18. DECLARE @sAllowed NVARCHAR(200)
  19. DECLARE @iLoop INT
  20. SET NOCOUNT ON
  21. IF @PrimaryTable IS NULL OR @PrimaryField IS NULL OR @ForeignTable IS NULL
  22. BEGIN
  23. INSERT INTO @Instructions (Info)
  24. VALUES (N'sp_CreateForeignKey')
  25. INSERT INTO @Instructions (Info)
  26. VALUES (N'Creates a primary key - foreign key relationship.')
  27. INSERT INTO @Instructions (Info)
  28. VALUES (N'sp_CreateForeignKey PrimaryTable,PrimaryField,ForeignTable,ForeignField')
  29. INSERT INTO @Instructions (Info)
  30. VALUES (N'EXAMPLE:')
  31. INSERT INTO @Instructions (Info)
  32. VALUES (N'EXEC sp_CreateForeignKey ''Countries'',''CountryID'',''Employees'',''CountryID''')
  33. INSERT INTO @Instructions (Info)
  34. VALUES (N'EXEC sp_CreateForeignKey ''Countries'',''CountryID'',''Employees''')
  35. INSERT INTO @Instructions (Info)
  36. VALUES (N'If ForeignField is omitted, PrimaryField is used as ForeignField.')
  37. SELECT [Info] As [Instructions]
  38. FROM @Instructions
  39. RETURN
  40. END
  41. IF LEN(ISNULL(@ForeignField,'')) = 0
  42. BEGIN
  43. SET @ForeignField = @PrimaryField
  44. END
  45. -- REMOVE ILLEGAL CHARS.
  46. SET @PrimaryTable = REPLACE(REPLACE(@PrimaryTable,N']',N''),N'[',N'')
  47. SET @PrimaryField = REPLACE(REPLACE(@PrimaryField,N']',N''),N'[',N'')
  48. SET @ForeignTable = REPLACE(REPLACE(@ForeignTable,N']',N''),N'[',N'')
  49. SET @ForeignField = REPLACE(REPLACE(@ForeignField,N']',N''),N'[',N'')
  50. -- CREATE @sConstraint
  51. SET @sConstraint = N'FK_' + @PrimaryTable + @PrimaryField + @ForeignTable + @ForeignField
  52. -- REMOVE ILLEGAL CHARS FROM @sConstraint
  53. SET @sAllowed = N'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_123457890'
  54. SET @iLoop = LEN(@sConstraint)
  55. WHILE @iLoop >= 1
  56. BEGIN
  57. IF CHARINDEX(SUBSTRING(@sConstraint,@iLoop,1),@sAllowed) = 0
  58. BEGIN
  59. SET @sConstraint = LEFT(@sConstraint,@iLoop-1) + SUBSTRING(@sConstraint,@iLoop+1,LEN(@sConstraint)-@iLoop)
  60. END
  61. SET @iLoop = @iLoop - 1
  62. END
  63. -- SHOW THE USER THE SQL TO RUN.
  64. SET @sSQL = N'ALTER TABLE [' + @ForeignTable + N'] ADD CONSTRAINT [' + @sConstraint + N'] FOREIGN KEY( [' + @ForeignField + N'] ) REFERENCES [' + @PrimaryTable + N'] ( [' + @PrimaryField + N'])'
  65. INSERT INTO @Instructions (Info)
  66. VALUES (N'--Copy the bottom line to the query window and run it.')
  67. INSERT INTO @Instructions (Info)
  68. VALUES (N'--Also, save it for when you move your changes into production.')
  69. INSERT INTO @Instructions (Info)
  70. VALUES (@sSQL)
  71. SELECT [Info] As [Instructions]
  72. FROM @Instructions
  73. END
  74. GO