[原创]批量表结构提取和批量建表[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 cb0912cn 的 blog


 fetch next from cursorname into @tablename1, @tabledescription, @columnname, @columndescription,@identityflag, @keyflag, @columntype, @typelength, @nullflag, @columndefault,@indexname
    if @@fetch_status<>0
     break
 if @tablename1 is not null and @tablename1<>' '
  select @tablename=@tablename1
 if @columntype='nvarchar' or @columntype='nchar'
  select @typelength=@typelength/2
 if @typelength>4000
  select @typelength=4000
 if @columntype='varchar' or @columntype='nvarchar' or @columntype='char' or @columntype='nchar'
  select @columntype=@columntype+'('+ltrim(str(@typelength))+')'
 if @nullflag='√'
  select @nullflag=''
 else
  select @nullflag='not null'
 if @columndefault<>''
  begin
  if @columntype='int' or @columntype='tinyint' or @columntype='smallint'
   select @columndefault='default '+@columndefault
  else
   select @columndefault='default '+char(39)+@columndefault+char(39)
  end

 if @identityflag='√'
  select @identityflag='identity(1,1)'
 else
  select @identityflag=''
 if @keyflag='√'
  select @keyflag='primary key '
 else
  select @keyflag=''
 
 
 if @tablename1 is not null and @tablename1<>' '
  begin
  print @tablename
  print  'create table '+ @tablename+'('+@columnname+' '+@columntype+' '+@nullflag+' '+@identityflag+
   ' '+ @keyflag+' '+@columndefault +')'
  if exists(select name from sysobjects where name=@tablename and type='u')
  exec ('drop table '+  @tablename)

  exec ('create table '+ @tablename+'('+@columnname+' '+@columntype+' '+@nullflag+' '+@identityflag+
   ' '+ @keyflag+' '+@columndefault +')')
  if @tabledescription is not null and @tabledescription<>' '
   exec sp_addextendedproperty  'ms_description', @tabledescription, 'user', dbo, 'table', @tablename
  if @columndescription is not null and @columndescription<>' '
   exec sp_addextendedproperty  'ms_description', @columndescription, 'user', dbo, 'table', @tablename, 'column', @columnname
  end
  
 else
  begin
  exec (' alter table '+@tablename +' add '+@columnname+' '+@columntype+' '+@identityflag+' '+@columndefault
  if @columndescription is not null and @columndescription<>' ' 
   exec sp_addextendedproperty  'ms_description', @columndescription, 'user', dbo, 'table', @tablename, 'column', @columnname
  if   @nullflag='not null'
   exec (' alter table '+@tablename +' alter column '+@columnname+' '+@columntype+ ' not null')
  if  @keyflag='primary key ' 
   exec (' alter table '+@tablename +' add constraint '+'pk_'+@tablename+'_'+@columnname+' '+' primary key('+@columnname+')')
&n

本文关键:[原创]批量表结构提取和批量建表
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top