sp_DropColumn.txt 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. CREATE PROCEDURE sp_DropColumn
  2. @TableName SYSNAME=NULL,
  3. @ColumnName SYSNAME=NULL
  4. AS
  5. BEGIN
  6. DECLARE @Constraints TABLE
  7. (
  8. [RowID] INT IDENTITY(1,1),
  9. [ConstraintName] SYSNAME
  10. );
  11. DECLARE @Info TABLE
  12. (
  13. ID INT IDENTITY(1,1),
  14. Info NVARCHAR(4000),
  15. PRIMARY KEY(ID)
  16. );
  17. DECLARE @iCount INT;
  18. DECLARE @iLoop INT;
  19. DECLARE @Constraint SYSNAME;
  20. DECLARE @sql NVARCHAR(4000);
  21. SET NOCOUNT ON;
  22. IF LEN(ISNULL(@TableName,'')) = 0 OR LEN(ISNULL(@ColumnName,'')) = 0
  23. BEGIN
  24. INSERT INTO @Info (Info) VALUES ('sp_DropColumn table,column');
  25. INSERT INTO @Info (Info) VALUES ('Displays the sql needed to drop a column.');
  26. INSERT INTO @Info (Info) VALUES ('Doesn`t actually drop the column.');
  27. INSERT INTO @Info (Info) VALUES ('Check out other cool tools like');
  28. INSERT INTO @Info (Info) VALUES ('sp_ColumnsInATable');
  29. INSERT INTO @Info (Info) VALUES ('sp_CreateUserDefinedType');
  30. INSERT INTO @Info (Info) VALUES ('sp_Find');
  31. INSERT INTO @Info (Info) VALUES ('sp_FindColumn');
  32. INSERT INTO @Info (Info) VALUES ('sp_FindInProc');
  33. INSERT INTO @Info (Info) VALUES ('sp_FindInAllProcs');
  34. INSERT INTO @Info (Info) VALUES ('sp_List');
  35. SELECT Info FROM @Info ORDER BY ID;
  36. RETURN;
  37. END;
  38. IF NOT EXISTS(SELECT SO.id FROM SYSOBJECTS AS SO
  39. INNER JOIN SYSCOLUMNS AS SC
  40. ON SO.id = SC.id
  41. WHERE SO.name = @TableName
  42. AND SC.name = @ColumnName
  43. AND SO.xtype = 'U')
  44. BEGIN
  45. INSERT INTO @Info (Info) VALUES ('-- Column ' + @ColumnName + ' wasn`t found in table ' + @TableName);
  46. SELECT Info FROM @Info ORDER BY ID;
  47. RETURN;
  48. END
  49. INSERT INTO @Constraints (ConstraintName)
  50. select dc.name as ConstraintName
  51. from sys.default_constraints as dc
  52. inner join sysobjects as so
  53. on dc.parent_object_id = so.id
  54. inner join syscolumns as sc
  55. on dc.parent_column_id = sc.colid
  56. and dc.parent_object_id = sc.id
  57. WHERE so.Name = @TableName
  58. AND sc.Name = @ColumnName;
  59. -- Get the list of foreign key constraints.
  60. WITH CTE_FOREIGN_KEYS (ConstraintName,ChildTable,ChildField,ParentTable,ParentField)
  61. AS
  62. (
  63. SELECT SON.name AS ConstraintName
  64. ,SOF.name AS ChildTable
  65. ,SCF.name AS ChildColumn
  66. ,SOR.name AS ParentTable
  67. ,SCR.name AS ParentColumn
  68. FROM SYSFOREIGNKEYS AS SF
  69. INNER JOIN SYSOBJECTS AS SON
  70. ON SF.constid = SON.id
  71. INNER JOIN SYSOBJECTS AS SOF
  72. ON SF.fkeyid = SOF.id
  73. INNER JOIN SYSOBJECTS AS SOR
  74. ON SF.rkeyid = SOR.id
  75. INNER JOIN SYSCOLUMNS AS SCF
  76. ON SF.fkeyid = SCF.id
  77. AND SF.fkey = SCF.colid
  78. INNER JOIN SYSCOLUMNS AS SCR
  79. ON SF.rkeyid = SCR.id
  80. AND SF.rkey = SCR.colid
  81. )
  82. INSERT INTO @Constraints (ConstraintName)
  83. SELECT ConstraintName
  84. FROM CTE_FOREIGN_KEYS
  85. WHERE (ChildTable = @TableName AND ChildField = @ColumnName)
  86. OR (ParentTable = @TableName AND ParentField = @TableName)
  87. AND ConstraintName NOT IN (SELECT ConstraintName FROM @Constraints);
  88. SELECT @iCount = COUNT(*) FROM @Constraints;
  89. SET @iLoop = 1
  90. WHILE @iLoop <= @iCount
  91. BEGIN
  92. SELECT @Constraint = ConstraintName
  93. FROM @Constraints
  94. WHERE RowID = @iLoop;
  95. SET @sql = 'ALTER TABLE [' + @TableName + '] DROP CONSTRAINT [' + @Constraint + ']';
  96. INSERT INTO @Info (Info) VALUES (@sql);
  97. -- exec sp_executesql @sql
  98. SET @iLoop = @iLoop + 1;
  99. END;
  100. SET @sql = 'ALTER TABLE [' + @TableName + '] DROP COLUMN [' + @ColumnName + ']';
  101. INSERT INTO @Info (Info) VALUES (@sql);
  102. SELECT Info FROM @Info ORDER BY ID;
  103. --exec sp_executesql @sql
  104. END
  105. GO