sp_ColumnsInATable.txt 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. CREATE PROCEDURE sp_ColumnsInATable
  2. (@sTable SYSNAME = NULL)
  3. AS
  4. DECLARE @sColumns VARCHAR(8000)
  5. DECLARE @sColumn SYSNAME
  6. DECLARE @bHasSpace BIT
  7. DECLARE @ColumnList TABLE
  8. (
  9. [ColumnName] VARCHAR(8000)
  10. );
  11. DECLARE @Info TABLE
  12. (
  13. ID INT IDENTITY(1,1),
  14. Info VARCHAR(500),
  15. PRIMARY KEY(ID)
  16. );
  17. SET NOCOUNT ON
  18. IF @sTable IS NULL
  19. BEGIN
  20. INSERT INTO @Info (Info) VALUES ('sp_ColumnsInATable {TableName}');
  21. INSERT INTO @Info (Info) VALUES ('Lists the columns in {TableName}');
  22. INSERT INTO @Info (Info) VALUES ('Lists them in a nice, easy to copy format.');
  23. INSERT INTO @Info (Info) VALUES ('Check out other cool tools like');
  24. INSERT INTO @Info (Info) VALUES ('sp_CreateUserDefinedType')
  25. INSERT INTO @Info (Info) VALUES ('sp_DropColumn')
  26. INSERT INTO @Info (Info) VALUES ('sp_Find')
  27. INSERT INTO @Info (Info) VALUES ('sp_FindColumn')
  28. INSERT INTO @Info (Info) VALUES ('sp_FindInProc')
  29. INSERT INTO @Info (Info) VALUES ('sp_FindInAllProcs')
  30. INSERT INTO @Info (Info) VALUES ('sp_List')
  31. INSERT INTO @Info (Info) VALUES ('sp_varinsp')
  32. INSERT INTO @Info (Info) VALUES ('Choose a table:');
  33. INSERT INTO @Info (Info)
  34. SELECT O.[name]
  35. from sysobjects as O
  36. WHERE O.xtype='U'
  37. ORDER BY O.[name];
  38. SELECT Info FROM @Info ORDER BY ID;
  39. END ELSE
  40. BEGIN
  41. SET @sColumns = ''
  42. DECLARE ColumnCursor CURSOR FOR
  43. SELECT distinct SC.[name] as ColumnName
  44. FROM sysobjects AS O
  45. INNER JOIN syscolumns as SC
  46. ON O.id=SC.id
  47. INNER JOIN systypes as ST
  48. ON SC.xtype = ST.xtype
  49. WHERE O.xtype='U'
  50. AND O.[name] = @sTable
  51. OPEN ColumnCursor
  52. FETCH NEXT FROM ColumnCursor
  53. INTO @sColumn
  54. WHILE @@FETCH_STATUS = 0
  55. BEGIN
  56. IF LEN(@sColumns)>60
  57. BEGIN
  58. INSERT INTO @ColumnList (ColumnName)
  59. VALUES (@sColumns + ',')
  60. SET @sColumns = ''
  61. END
  62. IF LEN(@sColumns) > 0
  63. BEGIN
  64. SET @sColumns = @sColumns + ','
  65. END
  66. SET @bHasSpace = 0
  67. IF CHARINDEX(' ',@sColumn) > 0
  68. BEGIN
  69. SET @bHasSpace = 1
  70. END
  71. IF CHARINDEX('$',@sColumn) > 0
  72. BEGIN
  73. SET @bHasSpace = 1
  74. END
  75. IF CHARINDEX(',',@sColumn) > 0
  76. BEGIN
  77. SET @bHasSpace = 1
  78. END
  79. IF CHARINDEX('#',@sColumn) > 0
  80. BEGIN
  81. SET @bHasSpace = 1
  82. END
  83. IF @bHasSpace <> 0
  84. BEGIN
  85. SET @sColumns = @sColumns + '['
  86. END
  87. SET @sColumns = @sColumns + @sColumn
  88. IF @bHasSpace <> 0
  89. BEGIN
  90. SET @sColumns = @sColumns + ']'
  91. END
  92. FETCH NEXT FROM ColumnCursor
  93. INTO @sColumn
  94. END
  95. IF LEN(@sColumns)>0
  96. BEGIN
  97. INSERT INTO @ColumnList (ColumnName)
  98. VALUES (@sColumns)
  99. END
  100. CLOSE ColumnCursor
  101. DEALLOCATE ColumnCursor
  102. SELECT ColumnName
  103. FROM @ColumnList
  104. END
  105. GO