欢迎光临
我们一直在努力

根据基本表结构及其数据生成 INSERT … 的 SQL-数据库专栏,SQL Server

建站超值云服务器,限时71元/月

create  proc spgeninsertsql
@tablename as varchar(100)
as
–declare @tablename varchar(100)
–set @tablename = orders
–set @tablename = eeducation
declare xcursor cursor for
select name,xusertype
from syscolumns
where (id = object_id(@tablename))
declare @f1 varchar(100)
declare @f2 integer
declare @sql varchar(8000)
set @sql =select insert into + @tablename + values(
open xcursor
fetch xcursor into @f1,@f2
while @@fetch_status = 0
begin
    set @sql =@sql +
              + case when @f2 in (35,58,99,167,175,231,239,61) then + case when + @f1 + is null then else end +   else + end
              + replace(isnull(cast( + @f1 + as varchar),null),,)
              + case when @f2 in (35,58,99,167,175,231,239,61) then + case when + @f1 + is null then else end +   else + end
              + char(13) + ,
    fetch next from xcursor into @f1,@f2
end
close xcursor
deallocate xcursor
set @sql = left(@sql,len(@sql) – 5) + + ) from + @tablename
print @sql
exec (@sql)

第二版:2003.03.08

alter proc spgeninsertsql (@tablename varchar(256))
as
begin
  declare @sql varchar(8000)
  declare @sqlvalues varchar(8000)
  set @sql = (
  set @sqlvalues = values (+
  select @sqlvalues = @sqlvalues + cols + + , + ,@sql = @sql + [ + name + ],
    from
        (select case
                  when xtype in (48,52,56,59,60,62,104,106,108,122,127)                               
                       then case when + name + is null then null else + cast(+ name + as varchar)+ end
                  when xtype in (58,61)
                       then case when + name + is null then null else + + + cast(+ name + as varchar)+ ++ end
           when xtype in (167)
                       then case when + name + is null then null else + + + replace(+ name+,,) + ++ end
                  when xtype in (231)
                       then case when + name + is null then null else +n + + replace(+ name+,,) + ++ end
                  when xtype in (175)
                       then case when + name + is null then null else + + + cast(replace(+ name+,,) as char( + cast(length as varchar)  + ))++ end
                  when xtype in (239)
                       then case when + name + is null then null else +n + + cast(replace(+ name+,,) as char( + cast(length as varchar)  + ))++ end
                  else null
                end as cols,name
           from syscolumns 
          where id = object_id(@tablename)
        ) t
  set @sql =select insert into [+ @tablename + ] + left(@sql,len(@sql)-1)+) + left(@sqlvalues,len(@sqlvalues)-4) + ) from +@tablename
  –print @sql
  exec (@sql)
end

第三版: 2003.3.9

alter   proc spgeninsertsql (@tablename varchar(256))
as
begin
  declare @sql varchar(8000)
  declare @sqlvalues varchar(8000)
  set @sql = (
  set @sqlvalues = values (+
  select @sqlvalues = @sqlvalues + cols + + , + ,@sql = @sql + [ + name + ],
    from
        (select case
                  when xtype in (48,52,56,59,60,62,104,106,108,122,127)                               
                       then case when + name + is null then null else + cast(+ name + as varchar)+ end
                  when xtype in (58,61)
                       then case when + name + is null then null else + + + cast(+ name + as varchar)+ ++ end
           when xtype in (167,175)
                       then case when + name + is null then null else + + + replace(+ name+,,) + + + end
                  when xtype in (231,239)
                       then case when + name + is null then null else +n + + replace(+ name+,,) + + + end
                  else null
                end as cols,name
           from syscolumns 
          where id = object_id(@tablename) and autoval is null
        ) t
  set @sql =select insert into [+ @tablename + ] + left(@sql,len(@sql)-1)+) + left(@sqlvalues,len(@sqlvalues)-4) + ) from +@tablename
  print @sql
  exec (@sql)
/*
select *
from syscolumns 
where id = object_id(test) and autoval is null
*/
end

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 根据基本表结构及其数据生成 INSERT … 的 SQL-数据库专栏,SQL Server
分享到: 更多 (0)

相关推荐

  • 暂无文章