博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
根据字段表 自动创建 表SQL
阅读量:6678 次
发布时间:2019-06-25

本文共 3090 字,大约阅读时间需要 10 分钟。

 

create  PROC  CreateTableSql@TableName varchar(50)ASdeclare @FieldName varchar(50) ,@FieldDescrible varchar(50) ,@DbType varchar(50) declare @DbLength varchar(50) ,@DbDecimal  varchar(50) ,@Default varchar(50) ,@IsIdentity bit ,@IsNull bit ,@IsPrimaryKey bit declare @CreateSql varchar(8000),@AddFieldSql varchar(300),@FieldSql varchar(3000)declare @PrimaryKey varchar(200) ,@NULL varchar(10) ,@Identity varchar(20),@PropertySql varchar(8000)set @PrimaryKey='' set @FieldSql=''SET @PropertySql=''DECLARE MyCursor CURSOR FOR  select  FieldName,isnull(FieldDescrible,''),isnull(DbType,''),DbLength,DbDecimal,isnull([Default],''),IsIdentity,  isnull([IsNull],0),IsPrimaryKey   from Fields   where TableName =@TableName order by SortNO OPEN MyCursor FETCH NEXT FROM MyCursor INTO @FieldName,@FieldDescrible,@DbType,@DbLength,@DbDecimal,@Default,@IsIdentity,@IsNull,@IsPrimaryKeyWHILE @@FETCH_STATUS = 0BEGINif @IsNull=1   set @NULL =' NULL'   ELSE set @NULL =' NOT  NULL' if @IsPrimaryKey =1  set @NULL =' NOT  NULL'   if @DbType in ('uniqueidentifier','int','datetime','timestamp','smallint') set @DbLength=''   else set @DbLength='('+@DbLength+')'if @IsIdentity=1  set @Identity =' IDENTITY(1,1) ' else set @Identity =''if @Default=''  set @AddFieldSql = '  ['+ @FieldName+'] ' + @DbType +@DbLength +@Identity+ @Null +','else   set @AddFieldSql = '  ['+ @FieldName+'] ' + @DbType +@DbLength +@Identity+ @Null +' default '+@Default +',' if @FieldSql=''    set @FieldSql= @AddFieldSql else    set @FieldSql= @FieldSql+ CHAR(10) +@AddFieldSql  if @IsPrimaryKey =1begin if @PrimaryKey<>'' set @PrimaryKey= @PrimaryKey+'['+ @FieldName+'] '  if @PrimaryKey='' set @PrimaryKey= '['+ @FieldName+'] ' end SET @PropertySql=@PropertySql+CHAR(10)+ ' GO'+ CHAR(10)+ 'EXEC sys.sp_addextendedproperty @name=N'+''''+'MS_Description'+''''+', @value=N'+''''+@FieldDescrible +''''+', @level0type=N'+''''+'SCHEMA'+''''     +',@level0name=N'+''''+'dbo'+'''' +', @level1type=N'+''''+'TABLE'+''''+',@level1name=N'+''''+@TableName+''''+', @level2type=N'+''''+'COLUMN'+''''+',@level2name=N'+''''+@FieldName+'''' FETCH NEXT FROM MyCursor INTO @FieldName,@FieldDescrible,@DbType,@DbLength,@DbDecimal,@Default,@IsIdentity,@IsNull,@IsPrimaryKeyENDCLOSE MyCursor;DEALLOCATE MyCursor;set @CreateSql =' if exists (select * from sysobjects where name='''+@TableName +''')'set @CreateSql=@CreateSql+  CHAR(10) + ' drop table   '+@TableNameset @CreateSql=@CreateSql+  CHAR(10) + ' GO   'set @CreateSql=@CreateSql+  CHAR(10) + ' CREATE TABLE  '+@TableName+  CHAR(10)+'('set @CreateSql=@CreateSql+  CHAR(10) + ''+@FieldSqlset @CreateSql=@CreateSql+  CHAR(10) + '  CONSTRAINT [PK_'+ @TableName +'] PRIMARY KEY CLUSTERED  ('set @CreateSql=@CreateSql+  CHAR(10) +'  '+ @PrimaryKey+' ASC  '+  CHAR(10)+'  )'set @CreateSql=@CreateSql+  CHAR(10) + '  WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 'set @CreateSql=@CreateSql+  CHAR(10) +' ) ON [PRIMARY] 'PRINT @CreateSqlprint @PropertySql
View Code

 

转载于:https://www.cnblogs.com/lhyqzx/p/6863251.html

你可能感兴趣的文章
zabbix + RedHat7 安装配置指导
查看>>
Linux基础命令---显示主机名hostname
查看>>
ASP后门、***清理
查看>>
strtus2的xml文件配置
查看>>
Error:No suitable device found: no device found for connection
查看>>
SCCM 2016 为客户端分发管理组件Configuration Manager(一)
查看>>
CentOS 7 多网卡绑定
查看>>
HIVE创建HBASE表
查看>>
k3cloud单据插件
查看>>
MaridDB主从复制,双主模型,半同步的配置
查看>>
麒麟开源堡垒机功能版本说明及升级方式说明
查看>>
交换机SPAN功能配置
查看>>
关于ssh的问题
查看>>
作业 rh124
查看>>
Docker创建tomcat镜像
查看>>
Restful学习随笔
查看>>
2018区块链学习路线及大纲附Java,Python,初级高级,深入浅出视频教程
查看>>
[Algorithms] Longest Common Subsequence
查看>>
常见排序算法总结(含C/C++代码)
查看>>
CurrentRowColor 选中行 颜色改变
查看>>