千家信息网

automatic generate create table sql

发表于:2025-01-21 作者:千家信息网编辑
千家信息网最后更新 2025年01月21日,点击(此处)折叠或打开--CREATE PROCEDURE gen_createtbl @tbl_name varchar(100)--ASset nocount on--声明游标需要的变量decla
千家信息网最后更新 2025年01月21日automatic generate create table sql

点击(此处)折叠或打开

  1. --CREATE PROCEDURE gen_createtbl @tbl_name varchar(100)
  2. --AS
  3. set nocount on
  4. --声明游标需要的变量
  5. declare @tblname varchar(100),
  6. @colno int,
  7. @colname varchar(100),
  8. @IsIdt bit,
  9. @ispk bit,
  10. @type varchar(100),
  11. @length int,
  12. @decim int,
  13. @isnull bit,
  14. @default varchar(100),
  15. @sql varchar(2000),--for create table
  16. @sql2 varchar(1000),--for create pk
  17. @sql3 varchar(1000), --for create CONSTRAINT
  18. @sql4 varchar(1000),-- for default value
  19. @sql5 varchar(1000),--for col comments
  20. @sql6 varchar(1000),--for table comments
  21. @tbl varchar(100),
  22. @idx varchar(100),
  23. @idxp varchar(100),
  24. @colname2 varchar(100),
  25. @comments varchar(100),--注释
  26. @tbcomments varchar(100),
  27. @col_id int,--索引中该字段的排列位置
  28. @col_num int,--索引包含的总列数
  29. @idx_type_desc varchar(100), --索引类型描述
  30. @is_unique bit --是否唯一
  31. set @tblname='sbj_retail_store_info'
  32. set @sql4=''
  33. --声明一个游标mycursor,select语句中参数的个数必须要和从游标取出的变量名相同
  34. declare mycursor cursor for
  35. SELECT 表名 = case when a.colorder=1 then d.name else '' end,
  36. 表说明 = cast((case when a.colorder=1 then isnull(f.value,'') else '' end) as varchar(100)),
  37. 字段序号 = a.colorder,
  38. 字段名 = a.name,
  39. 标识 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '1'else '' end, --IDENTITY(1,1)
  40. 主键 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
  41. SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then 1 else 0 end,
  42. 类型 = b.name,
  43. --占用字节数 = a.length,
  44. 长度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
  45. 小数位数 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
  46. 允许空 = case when a.isnullable=1 then '1'else '' end,
  47. 默认值 = isnull(e.text,''),
  48. 字段说明 = cast(isnull(g.[value],'') as varchar(100))
  49. FROM syscolumns a
  50. left join systypes b on a.xusertype=b.xusertype
  51. inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
  52. left join syscomments e on a.cdefault=e.id
  53. left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id and g.name='MS_Description'
  54. left join sys.extended_properties f on d.id=f.major_id and f.minor_id=0 and f.name='MS_Description'
  55. where d.name =@tblname --如果只查询指定表,加上此条件,表名
  56. order by a.id,a.colorder
  57. /*create temp table to get the comments*/
  58. create table #comtmp(
  59. [sql] varchar(3000)
  60. )
  61. /*create temp table to get index info and order*/
  62. create table #idxtmp(
  63. [tb_name] varchar(100),
  64. [idx_name] varchar(100),
  65. [col_name] varchar(100),
  66. [col_id] int,
  67. [idx_type_desc] varchar(100),
  68. [is_unique] bit)

  69. insert into #idxtmp
  70. SELECT
  71. tab.name AS [tb_name],--[表名],
  72. idx.name AS [idx_name],--[约束名称],
  73. col.name AS [col_name],--[约束列名],
  74. idxCol.key_ordinal AS [col_id],--[索引列顺序]
  75. idx.type_desc as[idx_type_desc], --[索引类型描述]
  76. idx.is_unique AS [is_unique] --[是否唯一]
  77. FROM
  78. sys.indexes idx
  79. JOIN sys.index_columns idxCol
  80. ON (idx.object_id = idxCol.object_id
  81. AND idx.index_id = idxCol.index_id
  82. AND idx.is_unique_constraint = 1)
  83. JOIN sys.tables tab
  84. ON (idx.object_id = tab.object_id)
  85. JOIN sys.columns col
  86. ON (idx.object_id = col.object_id
  87. AND idxCol.column_id = col.column_id)
  88. where tab.name=@tblname



  89. declare mycursor2 cursor for
  90. select a.[tb_name],a.[idx_name],a.[col_name],a.[col_id],b.[col_num],a.[idx_type_desc],a.[is_unique]
  91. from #idxtmp a
  92. left join (select [tb_name], [idx_name],count(1) col_num from #idxtmp group by [tb_name], [idx_name]) b
  93. on a.tb_name=b.tb_name
  94. and a.idx_name=b.[idx_name]

  95. --打开游标
  96. open mycursor
  97. --从游标里取出数据赋值到我们刚才声明的2个变量中
  98. fetch next from mycursor into @tblname,@tbcomments,@colno,@colname,@IsIdt,@ispk,@type,@length,@decim,@isnull,@default,@comments

  99. --判断游标的状态
  100. -- 0 fetch语句成功
  101. ---1 fetch语句失败或此行不在结果集中
  102. ---2 被提取的行不存在
  103. while (@@fetch_status=0)
  104. begin
  105. --显示出我们每次用游标取出的值
  106. --print '游标成功取出一条数据'
  107. if @colno=1
  108. begin
  109. set @tbl=@tblname
  110. set @sql='CREATE TABLE [dbo].['+@tblname+'](
  111. ['+@colname+'] ['+@type+'] '+(case @isnull when 0 then 'NOT NULL,' else 'NULL,'end)
  112. set @sql6='EXEC sys.sp_addextendedproperty @name=N'+''''+'MS_Description'+''',@value=N'+''''+@tbcomments+''',@level0type=N'+'''SCHEMA'+''',@level0name=N'+'''dbo'
  113. +''', @level1type=N'+'''TABLE'+''',@level1name=N'+''''+@tbl+''''
  114. insert into #comtmp([sql]) values (@sql6)
  115. -- print @sql6
  116. set @sql5='EXEC sys.sp_addextendedproperty @name=N'+'''MS_Description'+''', @value=N'+''''+@comments+''',@level0type=N'+''''+'SCHEMA'+''',@level0name=N'
  117. +'''dbo'+''',@level1type=N'+''''+'TABLE'+''''+',@level1name=N'+''''+@tbl+''', @level2type=N'+''''+'COLUMN'+''','+'@level2name=N'+''''+@colname+''''
  118. insert into #comtmp([sql]) values (@sql5)
  119. -- print @sql5


  120. end
  121. else
  122. begin
  123. set @tbl=@tbl+''
  124. --去掉ETL_CRC QA_RULE_CHK_FLG QA_MANUAL_FLG CREATE_BY UPDATE_BY 这五个字段
  125. if @colname in('ETL_CRC','QA_RULE_CHK_FLG','QA_MANUAL_FLG','CREATE_BY','UPDATE_BY')
  126. begin
  127. set @sql=@sql+''
  128. end
  129. else
  130. begin
  131. set @sql=@sql+'
  132. '+'['+@colname+'] ['+(case @type
  133. when 'timestamp' then 'bigint'+']'
  134. when 'varchar' then @type +']' +'('+cast(@length as varchar(10))+')'
  135. when 'nvarchar' then @type +']' +'('+cast(@length as varchar(10))+')'
  136. when 'char' then @type +']' +'('+cast(@length as varchar(10))+')'
  137. when 'decimal' then @type +']'+'('+cast(@length as varchar(10))+','+cast(@decim as varchar(3))+')'
  138. else @type+']'end )+
  139. (case @isnull when 0 then ' NOT NULL,' else ' NULL,'end)
  140. set @sql5='EXEC sys.sp_addextendedproperty @name=N'+'''MS_Description'+''', @value=N'+''''+@comments+''',@level0type=N'+''''+'SCHEMA'+''',@level0name=N'
  141. +'''dbo'+''',@level1type=N'+''''+'TABLE'+''''+',@level1name=N'+''''+@tbl+''', @level2type=N'+''''+'COLUMN'+''','+'@level2name=N'+''''+@colname+''''
  142. -- print @sql5
  143. insert into #comtmp([sql]) values (@sql5)
  144. end
  145. end

  146. If @ispk=1
  147. begin
  148. set @sql2='PRIMARY KEY CLUSTERED
  149. (
  150. ['+@colname+'] ASC
  151. ))
  152. GO'
  153. end
  154. else
  155. begin
  156. set @sql2=@sql2+''
  157. end

  158. If @default <>'' and @colname not in('ETL_CRC','QA_RULE_CHK_FLG','QA_MANUAL_FLG','CREATE_BY','UPDATE_BY')
  159. begin
  160. set @sql4=@sql4+'
  161. ALTER TABLE [dbo].['+@tbl+'] ADD DEFAULT '+@default+' FOR ['+@colname+']
  162. GO'

  163. end
  164. else
  165. begin
  166. set @sql4=@sql4+''
  167. end




  168. --用游标去取下一条记录 -
  169. fetch next from mycursor into @tblname,@tbcomments,@colno,@colname,@IsIdt,@ispk,@type,@length,@decim,@isnull,@default,@comments
  170. end
  171. --关闭游标
  172. close mycursor
  173. --撤销游标
  174. DEALLOCATE mycursor

  175. print @sql
  176. print @sql2
  177. print @sql4
  178. --声明一个游标mycursor,select语句中参数的个数必须要和从游标取出的变量名相同


  179. --打开游标
  180. set @idx=0
  181. open mycursor2
  182. --从游标里取出数据赋值到我们刚才声明的2个变量中
  183. fetch next from mycursor2 into @tblname,@idx,@colname2,@col_id,@col_num,@idx_type_desc,@is_unique

  184. --判断游标的状态
  185. -- 0 fetch语句成功
  186. ---1 fetch语句失败或此行不在结果集中
  187. ---2 被提取的行不存在
  188. while (@@fetch_status=0)
  189. begin
  190. --显示出我们每次用游标取出的值

  191. if @idx<>isnull(@idxp,'')
  192. begin
  193. set @sql3='ALTER TABLE [dbo].['+@tblname+'] ADD CONSTRAINT ['+@idx+'] '+(case when @is_unique=1 then'UNIQUE 'else '' end) +@idx_type_desc+'
  194. ( ['+@colname2+'] ASC'
  195. end
  196. else
  197. begin
  198. set @sql3=@sql3+'
  199. ['+@colname2+'] ASC'
  200. end

  201. if @col_id< @col_num
  202. begin
  203. set @sql3=@sql3+' ,'
  204. end
  205. else
  206. begin
  207. set @sql3=@sql3+')'
  208. print @sql3
  209. end



  210. --用游标去取下一条记录 -
  211. set @idxp=@idx
  212. fetch next from mycursor2 into @tblname,@idx,@colname2,@col_id,@col_num,@idx_type_desc,@is_unique

  213. end
  214. --关闭游标
  215. close mycursor2
  216. --撤销游标
  217. DEALLOCATE mycursor2
  218. if object_id('tempdb..#idxtmp') is not null
  219. begin
  220. --select * from #idxtmp
  221. drop table #idxtmp
  222. end
  223. select * from #comtmp
  224. drop table #comtmp
  225. set nocount on
  226. GO

0