Don’t you like to have a stored procedure handy like sp_helptext to generate table definitions for you from the query window without switching over to the SSMS object explorer? Here it is, I have created a stored procedure called GetTableCreateScript. Using this procedure, you can get the table definitions including primary key, unique and non-clustered indexes on the query window itself like sp_helptext

How to Use it.

Open SQL Server management studio, select appropriate database and create “GetTableCreateScript" stored procedure. After creating procedure, run the stored procedure as mentioned in the below image.


Below is entire script.
  1. aLTER Proc GetTableCreateScript
  2. (@TableName varchar(150))
  3. as
  4. Begin
  5. /*
  6. Name: Export Table Definition
  7. Developer: Nisarg Upadhyay
  8. Description:
  9. 1.This script will generate Table defination in text format. It will include
  10. 1. Table Definition
  11. 2. Unique, Default and Primary key Constraint
  12. 3. Indexes
  13. Version: v1.0
  14. */
  15. /*Check weather table is present or not*/
  16. If not exists (select name from sys.tables where name =@TableName) begin Print 'Table do not exists.' Return end
  17. /*Prepare temp table to keep entire table defination at one place*/
  18. Create Table #TableDefination (s nvarchar(max), id int identity)
  19. /*Insert Create Table staement*/
  20. Insert Into #TableDefination Values ('Create Table ' + @TableName + '(')
  21. /*Columns*/
  22. Insert Into #TableDefination
  23. select
  24. '['+column_name+'] ' +
  25. data_type + case when character_maximum_length=-1 then '(' + 'max' + ')' else coalesce('('+cast(character_maximum_length as varchar)+')','') End + ' ' +
  26. case when exists ( select a.name from sys.identity_columns a inner join sys.tables b on a.object_id=b.object_id and b.name=@TableName and a.name=COLUMN_NAME ) then 'IDENTITY(' + ( select convert(varchar,seed_value) from sys.identity_columns a inner join sys.tables b on a.object_id=b.object_id and b.name=@TableName) + ',' + ( select convert(varchar,increment_value) from sys.identity_columns a inner join sys.tables b on a.object_id=b.object_id and b.name=@TableName) + ')' else '' end + ' ' +','
  27. from information_schema.columns where table_name = @TableName
  28. order by ordinal_position
  29. /*Close bracket*/
  30. update #TableDefination set s=left(s,len(s)-1) where id=@@identity
  31. insert into #TableDefination(s) values (' )')
  32. /*Constraint: Default*/
  33. if exists (SELECT Col.Column_Name,Tab.CONSTRAINT_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab, INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col WHERE Col.Constraint_Name = Tab.Constraint_Name AND Col.Table_Name = Tab.Table_Name AND Constraint_Type = 'DEFAULT' AND Col.Table_Name = @TableName)
  34. Begin
  35. Insert into #TableDefination
  36. Select 'Alter Table [' + @TableName + '] Add Constraint [' + a.Name+'] DEFAULT '+ definition +' FOR ['+ (select name from sys.columns where object_id=b.object_id and column_id=a.parent_column_id ) +']' from sys.default_constraints a inner join sys.tables b on a.parent_object_id=b.object_id where b.name=@TableName
  37. End
  38. /*Constraint: Primary Key*/
  39. if exists (SELECT Col.Column_Name,Tab.CONSTRAINT_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab, INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col WHERE Col.Constraint_Name = Tab.Constraint_Name AND Col.Table_Name = Tab.Table_Name AND Constraint_Type = 'PRIMARY KEY' AND Col.Table_Name = @TableName)
  40. Begin
  41. Insert into #TableDefination Select 'Alter Table [' + @TableName + '] Add Constraint [' + Tab.CONSTRAINT_NAME+'] Primary Key ('+ Col.COLUMN_NAME +')' from INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab, INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col where Col.Constraint_Name = Tab.Constraint_Name AND Col.Table_Name = Tab.Table_Name AND Constraint_Type = 'PRIMARY KEY' and Tab.Table_name=@TableName
  42. End
  43. /*Constraint: Unique*/
  44. if exists (SELECT Col.Column_Name,Tab.CONSTRAINT_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab, INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col WHERE Col.Constraint_Name = Tab.Constraint_Name AND Col.Table_Name = Tab.Table_Name AND Constraint_Type = 'UNIQUE' AND Col.Table_Name = @TableName)
  45. Begin
  46. Insert into #TableDefination Select 'Alter Table [' + @TableName + '] Add Constraint [' + Tab.CONSTRAINT_NAME+'] UNIQUE ('+ Col.COLUMN_NAME +')' from INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab, INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col where Col.Constraint_Name = Tab.Constraint_Name AND Col.Table_Name = Tab.Table_Name AND Constraint_Type = 'UNIQUE' and Tab.Table_name=@TableName
  47. End
  48. /*Indexes*/
  49. Insert into #TableDefination
  50. select
  51. 'CREATE '+ i.[type_desc] + ' INDEX [' + I.NAME + '] ON [' + O.NAME + ']
  52. ( ' + COLUMNS.Normal +')'
  53. from sys.indexes i
  54. join sys.objects o on i.object_id = o.object_id
  55. cross apply
  56. (
  57. select
  58. substring
  59. (
  60. (
  61. select ', ' + co.[name]
  62. from sys.index_columns ic
  63. join sys.columns co on co.object_id = i.object_id and co.column_id = ic.column_id
  64. where ic.object_id = i.object_id and ic.index_id = i.index_id and ic.is_included_column = 0
  65. order by ic.key_ordinal
  66. for xml path('')
  67. )
  68. , 3
  69. , 10000
  70. ) as [Normal]
  71. , substring
  72. (
  73. (
  74. select ', ' + co.[name]
  75. from sys.index_columns ic
  76. join sys.columns co on co.object_id = i.object_id and co.column_id = ic.column_id
  77. where ic.object_id = i.object_id and ic.index_id = i.index_id and ic.is_included_column = 1
  78. order by ic.key_ordinal
  79. for xml path('')
  80. )
  81. , 3
  82. , 10000
  83. ) as [Included]
  84. ) Columns
  85. where o.[type] = 'U' --USER_TABLE
  86. and o.name =@TableName
  87. order by o.[name], i.[name], i.is_primary_key desc
  88. /*Final Select*/
  89. Select s from #TableDefination
  90. Drop Table #TableDefination
  91. End
Leave your valuable feedback.