织梦CMS - 轻松建站从此开始!

罗索

几个收藏的根据数据库生成Insert语句的存储过程

落鹤生 发布于 2010-08-19 16:55 点击:次 
落鹤生:非常有用的两个存储过程,可根据表中数据生成insert语句。使用方法:建立存储过程后,执行spGenInsertSQL "表名",即返回表中所有记录的Insert SQL语句。不过目前的版本未判断自增量的栏位。
TAG:

-- ======================================================
--根据表中数据生成insert语句的存储过程
--建立存储过程,执行spGenInsertSQL 表名
--感谢playyuer
----感谢szyicol
-- ======================================================

  1. CREATE   proc spGenInsertSQL 
  2.  
  3. (@tablename varchar(256)) 
  4.  
  5. as 
  6.  
  7. begin 
  8.  
  9.   declare @sql varchar(8000) 
  10.  
  11.   declare @sqlValues varchar(8000) 
  12.   set @sql =' (' 
  13.   set @sqlValues = 'values (''+' 
  14.   select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + '[' + name + '],' 
  15.     from 
  16.         (select case 
  17.                   when xtype in (48,52,56,59,60,62,104,106,108,122,127)
  18.                        then 'case when 'name +' is null then ''NULL'' else '
  19.  + 'cast('name + ' as varchar)'+' end'
  20.                   when xtype in (58,61)
  21.                        then 'case when 'name +' is null then ''NULL'' else '+''''''''' + '
  22.  + 'cast('name +' as varchar)''+'''''''''+' end' 
  23.                  when xtype in (167)
  24.                        then 'case when 'name +' is null then ''NULL'' else '+''''''''' + '
  25.  + 'replace('name+','''''''','''''''''''')' + '+'''''''''+' end' 
  26.                   when xtype in (231) 
  27.                        then 'case when 'name +' is null then ''NULL'' else '+'''N'''''' + '
  28.  + 'replace('name+','''''''','''''''''''')' + '+'''''''''+' end' 
  29.                   when xtype in (175)
  30.                        then 'case when 'name +' is null then ''NULL'' else '+''''''''' + '
  31.  + 'cast(replace('name+','''''''','''''''''''') as Char(' + cast(length as varchar)  + '))+'''''''''
  32. +' end' 
  33.                   when xtype in (239) 
  34.                        then 'case when 'name +' is null then ''NULL'' else '+'''N'''''' + '
  35.  + 'cast(replace('name+','''''''','''''''''''') as Char(' + cast(length as varchar)  + '))+'''''''''
  36. +' end' 
  37.                   else '''NULL''' 
  38.                 end as Cols,name 
  39.            from syscolumns  
  40.           where id = object_id(@tablename) 
  41.         ) T 
  42.   set @sql ='select ''INSERT INTO ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' 
  43. left(@sqlValues,len(@sqlValues)-4) + ')'' from '+@tablename 
  44.   --print @sql 
  45.   exec (@sql)
  46. end
  47. GO 

-- ======================================================
--根据表中数据生成insert语句的存储过程
--建立存储过程,执行proc_insert 表名
--感谢Sky_blue
--感谢szyicol
-- ======================================================

  1. CREATE proc proc_insert (@tablename varchar(256)) 
  2. as 
  3. begin 
  4.        set nocount on 
  5.        declare @sqlstr varchar(4000) 
  6.        declare @sqlstr1 varchar(4000) 
  7.        declare @sqlstr2 varchar(4000) 
  8.        select @sqlstr='select ''insert '+@tablename 
  9.   
  10.        select @sqlstr1='' 
  11.        select @sqlstr2=' (' 
  12.        select @sqlstr1= ' values ( ''+' 
  13.        select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case 
  14. --     when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else ' 
  15. -- +'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end' 
  16.        when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else ' 
  17. +'convert(varchar(1),'+a.name +')'+' end' 
  18.        when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else ' 
  19. +'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
  20.        when a.xtype =61  then 'case when '+a.name+' is null then ''NULL'' else ' 
  21. +'''''''''+'+'convert(varchar(23),'+a.name +',121)''+'''''''''+' end' 
  22.        when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else ' 
  23. +'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end' 
  24.        when a.xtype =62  then 'case when '+a.name+' is null then ''NULL'' else ' 
  25. +'convert(varchar(23),'+a.name +',2)'+' end' 
  26.        when a.xtype =56  then 'case when '+a.name+' is null then ''NULL'' else ' 
  27. +'convert(varchar(11),'+a.name +')'+' end' 
  28.        when a.xtype =60  then 'case when '+a.name+' is null then ''NULL'' else ' 
  29. +'convert(varchar(22),'+a.name +')'+' end' 
  30.        when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else ' 
  31. +'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
  32.        when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else ' 
  33. +'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end' 
  34.        when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else ' 
  35. +'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
  36.        when a.xtype =59  then 'case when '+a.name+' is null then ''NULL'' else ' 
  37. +'convert(varchar(23),'+a.name +',2)'+' end' 
  38.        when a.xtype =58  then 'case when '+a.name+' is null then ''NULL'' else ' 
  39. +'''''''''+'+'convert(varchar(23),'+a.name +',121)''+'''''''''+' end' 
  40.        when a.xtype =52  then 'case when '+a.name+' is null then ''NULL'' else ' 
  41. +'convert(varchar(12),'+a.name +')'+' end' 
  42.        when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else ' 
  43. +'convert(varchar(22),'+a.name +')'+' end' 
  44.        when a.xtype =48  then 'case when '+a.name+' is null then ''NULL'' else ' 
  45. +'convert(varchar(6),'+a.name +')'+' end' 
  46. --     when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else ' 
  47. -- +'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end' 
  48.        when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else ' 
  49. +'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
  50.        else '''NULL''' 
  51.        end as col,a.colid,a.name 
  52.        from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 
  53.  and a.xtype <>34 and a.xtype <>35 and  a.xtype <>36 
  54.        )t order by colid 
  55.        select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') ' 
  56. +left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename 
  57. --  print @sqlstr 
  58.        exec( @sqlstr) 
  59.        set nocount off 
  60. end 
  61. GO 
(秩名)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201008/10002.html]
本文出处:网络博客 作者:秩名
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容